static PapercutContainer() { _rootLogger = new Lazy<ILogger>(() => { string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs", $"{"PapercutCoreFailure"}.json"); var jsonSink = new RollingFileSink(logFilePath, new JsonFormatter(), null, null); return new LoggerConfiguration().MinimumLevel.Debug() .Enrich.With<EnvironmentEnricher>() .WriteTo.ColoredConsole() .WriteTo.Sink(jsonSink, LogEventLevel.Debug).CreateLogger(); }); _containerProvider = new Lazy<IContainer>(Build, LazyThreadSafetyMode.ExecutionAndPublication); AppDomain.CurrentDomain.ProcessExit += DisposeContainer; AppDomain.CurrentDomain.UnhandledException += (sender, args) => { if (args.IsTerminating) _rootLogger.Value.Fatal(args.ExceptionObject as Exception, "Unhandled Exception"); else _rootLogger.Value.Information(args.ExceptionObject as Exception, "Non-Fatal Unhandled Exception"); }; }
//******************************************************************* // STATIC MEMBERS //******************************************************************* #region public static GoogleCloudPubSubSinkState Create(GoogleCloudPubSubSinkOptions options, RollingFileSink errorsRollingFileSink) { if (options == null) throw new ArgumentNullException("options"); else return new GoogleCloudPubSubSinkState(options, errorsRollingFileSink); }
public DurableSeqSink( string serverUrl, string bufferBaseFilename, string apiKey, int batchPostingLimit, TimeSpan period, long? bufferFileSizeLimitBytes, long? eventBodyLimitBytes, LoggingLevelSwitch levelControlSwitch, HttpMessageHandler messageHandler, long? retainedInvalidPayloadsLimitBytes) { if (serverUrl == null) throw new ArgumentNullException(nameof(serverUrl)); if (bufferBaseFilename == null) throw new ArgumentNullException(nameof(bufferBaseFilename)); _shipper = new HttpLogShipper( serverUrl, bufferBaseFilename, apiKey, batchPostingLimit, period, eventBodyLimitBytes, levelControlSwitch, messageHandler, retainedInvalidPayloadsLimitBytes); _sink = new RollingFileSink( bufferBaseFilename + "-{Date}.json", new RawJsonFormatter(), bufferFileSizeLimitBytes, null); }
public static LoggerConfiguration RollingFileAsJson(this LoggerSinkConfiguration sinkConfiguration, string pathFormat, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum) { if (sinkConfiguration == null) throw new ArgumentNullException("sinkConfiguration"); var jsonSink = new RollingFileSink(pathFormat, new JsonFormatter(false, null, true), DefaultFileSizeLimitBytes, DefaultRetainedFileCountLimit); return sinkConfiguration.Sink(jsonSink, restrictedToMinimumLevel); }
public static LoggerConfiguration RollingFileAsText(this LoggerSinkConfiguration sinkConfiguration, string pathFormat, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum) { if (sinkConfiguration == null) throw new ArgumentNullException("sinkConfiguration"); var formatter = new MessageTemplateTextFormatter(DefaultOutputTemplate, null); var sink = new RollingFileSink(pathFormat, formatter, DefaultFileSizeLimitBytes, DefaultRetainedFileCountLimit); return sinkConfiguration.Sink(sink, restrictedToMinimumLevel); }
public DurableSeqSink(string serverUrl, string bufferBaseFilename, string apiKey, int batchPostingLimit, TimeSpan period) { if (serverUrl == null) throw new ArgumentNullException("serverUrl"); if (bufferBaseFilename == null) throw new ArgumentNullException("bufferBaseFilename"); _shipper = new HttpLogShipper(serverUrl, bufferBaseFilename, apiKey, batchPostingLimit, period); _sink = new RollingFileSink( bufferBaseFilename + "-{Date}.json", new SeqJsonFormatter(trailingNewline: true), null, null); }
public static LoggerConfiguration FileSinkDefinedFromConfig(this LoggerSinkConfiguration loggerConfiguration) { var logDir = ConfigurationManager.AppSettings["LoggingDirectory"]; if (!Directory.Exists(logDir)) Directory.CreateDirectory(logDir); var filePath = Path.Combine(logDir, Constants.AppName + "-{Date}.log.json"); var maxSize = int.Parse(ConfigurationManager.AppSettings["LoggingFileSizeLimitMBytes"])*1048576; var retainLimit = int.Parse(ConfigurationManager.AppSettings["LoggingRetainedFileCountLimit"]); var minLevel = ConfigurationManager.AppSettings["LoggingMinLevel"]; var sink = new RollingFileSink(filePath, new CustomLogJsonFormatter(), maxSize, retainLimit); return loggerConfiguration.Sink(sink, (LogEventLevel)Enum.Parse(typeof(LogEventLevel), minLevel)); }
public DurableElasticsearchSink(ElasticsearchSinkOptions options) { _state = ElasticsearchSinkState.Create(options); if (string.IsNullOrWhiteSpace(options.BufferBaseFilename)) { throw new ArgumentException("Cannot create the durable ElasticSearch sink without a buffer base file name!"); } _sink = new RollingFileSink( options.BufferBaseFilename + FileNameSuffix, _state.DurableFormatter, options.BufferFileSizeLimitBytes, null); _shipper = new ElasticsearchLogShipper(_state); }
public DurableKinesisFirehoseSink(KinesisFirehoseSinkOptions options, IAmazonKinesisFirehose kinesisFirehoseClient) { var state = new KinesisSinkState(options, kinesisFirehoseClient); if (string.IsNullOrWhiteSpace(options.BufferBaseFilename)) { throw new ArgumentException("Cannot create the durable Amazon Kinesis Firehose sink without a buffer base file name."); } _sink = new RollingFileSink( options.BufferBaseFilename + "-{Date}.json", state.DurableFormatter, options.BufferFileSizeLimitBytes, null); _shipper = new HttpLogShipper(state); if (options.OnLogSendError != null) { _shipper.LogSendError += options.OnLogSendError; } }
/// <summary> /// Write log events to a series of files. Each file will be named according to /// the date of the first log entry written to it. Only simple date-based rolling is /// currently supported. /// </summary> /// <param name="sinkConfiguration">Logger sink configuration.</param> /// <param name="pathFormat">String describing the location of the log files, /// with {Date} in the place of the file date. E.g. "Logs\myapp-{Date}.log" will result in log /// files such as "Logs\myapp-2013-10-20.log", "Logs\myapp-2013-10-21.log" and so on.</param> /// <param name="restrictedToMinimumLevel">The minimum level for /// events passed through the sink.</param> /// <param name="outputTemplate">A message template describing the format used to write to the sink. /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <param name="fileSizeLimitBytes">The maximum size, in bytes, to which any single log file will be allowed to grow. /// For unrestricted growth, pass null. The default is 1 GB.</param> /// <param name="retainedFileCountLimit">The maximum number of log files that will be retained, /// including the current log file. For unlimited retention, pass null. The default is 31.</param> /// <returns>Configuration object allowing method chaining.</returns> /// <remarks>The file will be written using the UTF-8 character set.</remarks> public static LoggerConfiguration RollingFile( this LoggerSinkConfiguration sinkConfiguration, string pathFormat, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, string outputTemplate = DefaultOutputTemplate, IFormatProvider formatProvider = null, long? fileSizeLimitBytes = DefaultFileSizeLimitBytes, int? retainedFileCountLimit = DefaultRetainedFileCountLimit) { if (sinkConfiguration == null) throw new ArgumentNullException("sinkConfiguration"); if (outputTemplate == null) throw new ArgumentNullException("outputTemplate"); var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider); var sink = new RollingFileSink(pathFormat, formatter, fileSizeLimitBytes, retainedFileCountLimit); return sinkConfiguration.Sink(sink, restrictedToMinimumLevel); }
//******************************************************************* // CONSTRUCTOR //******************************************************************* #region private GoogleCloudPubSubSinkState(GoogleCloudPubSubSinkOptions options, RollingFileSink errorsRollingFileSink) { //--- Mandatory options validations -------------------- if (options.BatchPostingLimit < 1 ) throw new ArgumentException("batchPostingLimit must be >= 1"); if (string.IsNullOrWhiteSpace(options.ProjectId)) throw new ArgumentException("options.ProjectId"); if (string.IsNullOrWhiteSpace(options.TopicId)) throw new ArgumentException("options.TopicId"); //--- // All is ok ... this._options = options; this._errorsRollingFileSink = errorsRollingFileSink; this._periodicBatchingFormatter = options.CustomFormatter ?? new GoogleCloudPubSubRawFormatter(); this._durableFormatter = options.CustomFormatter ?? new GoogleCloudPubSubRawFormatter(); this._topic = PublisherClient.FormatTopicName(options.ProjectId, options.TopicId); this._client = PublisherClient.Create(); //--- try { if (!string.IsNullOrWhiteSpace(this.Options.EventFieldSeparator) && !string.IsNullOrWhiteSpace(this.Options.MessageAttrMinValue)) { string[] auxArray = this.Options.MessageAttrMinValue.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries); if (auxArray.Length == 2) { this._attrMinPosition = Int32.Parse(auxArray[0]); this._attrMinName = auxArray[1]; this._attrMinCreate = true; } } } catch { this._attrMinCreate = false; } }
//-------------- private void Initialize(GoogleCloudPubSubSinkOptions options) { //--- Mandatory options validations -------------------- this.ValidateMandatoryOptions(options); //--- // All is ok ... instances are created using the defined options... if (!options.BufferFileExtension.StartsWith(".")) options.BufferFileExtension = "." + options.BufferFileExtension; //--- RollingFileSink to store internal errors ------------------ // It will be generated a file for each day. string errorsFileExtension = (options.BufferFileExtension == ".log" ? ".errorlog" : ".log"); if (!string.IsNullOrWhiteSpace(options.ErrorBaseFilename)) { this._errorsRollingFileSink = new RollingFileSink( options.ErrorBaseFilename + CNST_Specifier_Separator + options.ErrorRollingSpecifier + errorsFileExtension, new GoogleCloudPubSubRawFormatter(), // Formatter for error info (raw). options.ErrorFileSizeLimitBytes, options.ErrorRetainedFileCountLimit ); } //--- this._state = GoogleCloudPubSubSinkState.Create(options, this._errorsRollingFileSink); this._shipper = new GoogleCloudPubSubLogShipper(this._state); //--- //--- RollingFileSink to store data to be sent to PubSub ------------------ // It will be generated a file for each day. this._dataRollingFileSink = new RollingFileSink( options.BufferBaseFilename + CNST_Specifier_Separator + options.BufferRollingSpecifier + options.BufferFileExtension, this._state.DurableFormatter, // Formatter for data to insert into the buffer file. options.BufferFileSizeLimitBytes, null ); // NOTE: if the encoding is set to UTF8 then the BOM is inserted into the file and has to be // taken in mind when reading from the file. }