Пример #1
0
        /// <summary>
        /// Validate the options fo minimum data
        /// </summary>
        /// <param name="options">Logger options</param>
        protected virtual void ValidateOptions(ReadonlyLoggerOptions options)
        {
            if (options.SizePerFile.SizeInBytes <= 1000)
            {
                throw new ArgumentException("The file size have to be positive and at least 1000  bytes", nameof(options));
            }

            if (string.IsNullOrWhiteSpace(options.FileName))
            {
                throw new ArgumentException("The file name should be set", nameof(options));
            }
        }
Пример #2
0
 /// <summary>
 ///     Initialize from readonly options
 /// </summary>
 /// <param name="readOnlyOptions">Read only options</param>
 public static LoggerOptions CopyFromReadonly(ReadonlyLoggerOptions readOnlyOptions)
 {
     return(new LoggerOptions
     {
         FileName = readOnlyOptions.FileName,
         IsCompressed = readOnlyOptions.IsCompressed,
         Level = readOnlyOptions.Level,
         ArchiveCount = readOnlyOptions.ArchiveCount,
         SizePerFile = readOnlyOptions.SizePerFile,
         IsArchiveOnStart = readOnlyOptions.IsArchiveOnStart,
         Filter = readOnlyOptions.Filter,
         LogMessageLayout = readOnlyOptions.LogMessageLayout,
         LogToDebugStream = readOnlyOptions.LogToDebugStream
     });
 }
Пример #3
0
        /// <inheritdoc />
        public virtual void ApplyConfiguration(LoggerOptions options)
        {
            CurrentOptions = options ?? throw new ArgumentNullException(nameof(options), "Null options are not allowed");

            // Validate options
            ValidateOptions(CurrentOptions);

            // Reconfigure NLog
            var fileTarget = new NLog.Targets.FileTarget("FileLog")
            {
                FileName                = options.FileName,
                ArchiveNumbering        = NLog.Targets.ArchiveNumberingMode.DateAndSequence,
                ArchiveOldFileOnStartup = options.IsArchiveOnStart,
                Layout = options.LogMessageLayout ?? NLOG_LAYOUT,
                EnableArchiveFileCompression = options.IsCompressed,
                ArchiveAboveSize             = options.SizePerFile.SizeInBytes,
                MaxArchiveFiles = options.ArchiveCount
            };

            var configuration = new NLog.Config.LoggingConfiguration();

            configuration.AddTarget(fileTarget);

            // Configure rule
            var filter   = string.IsNullOrWhiteSpace(options.Filter) ? "*" : options.Filter;
            var fileRule = new NLog.Config.LoggingRule(filter, options.Level.ToNLog(), fileTarget);

            configuration.LoggingRules.Add(fileRule);

            // Debug
            if (options.LogToDebugStream)
            {
                var debugTarget = new NLog.Targets.DebuggerTarget("DebugLog")
                {
                    Layout = options.LogMessageLayout ?? NLOG_LAYOUT
                };
                configuration.AddTarget(debugTarget);
                var debugRule = new NLog.Config.LoggingRule(filter, options.Level.ToNLog(), debugTarget);
                configuration.LoggingRules.Add(debugRule);
            }

            // Set configuration
            NLog.LogManager.Configuration = configuration;
        }