コード例 #1
0
    /// <summary>
    /// Initializes <see cref="W3CLoggingMiddleware" />.
    /// </summary>
    /// <param name="next"></param>
    /// <param name="options"></param>
    /// <param name="w3cLogger"></param>
    public W3CLoggingMiddleware(RequestDelegate next, IOptionsMonitor <W3CLoggerOptions> options, W3CLogger w3cLogger)
    {
        if (next == null)
        {
            throw new ArgumentNullException(nameof(next));
        }

        if (options == null)
        {
            throw new ArgumentNullException(nameof(options));
        }

        if (w3cLogger == null)
        {
            throw new ArgumentNullException(nameof(w3cLogger));
        }

        _next      = next;
        _options   = options;
        _w3cLogger = w3cLogger;
        _additionalRequestHeaders = W3CLoggerOptions.FilterRequestHeaders(options.CurrentValue);
    }
コード例 #2
0
    public FileLoggerProcessor(IOptionsMonitor <W3CLoggerOptions> options, IHostEnvironment environment, ILoggerFactory factory)
    {
        _logger = factory.CreateLogger(typeof(FileLoggerProcessor));

        _options = options;
        var loggerOptions = _options.CurrentValue;

        _path = loggerOptions.LogDirectory;
        // If user supplies no LogDirectory, default to {ContentRoot}/logs.
        // If user supplies a relative path, use {ContentRoot}/{LogDirectory}.
        // If user supplies a full path, use that.
        if (string.IsNullOrEmpty(_path))
        {
            _path = Path.Join(environment.ContentRootPath, "logs");
        }
        else if (!Path.IsPathRooted(_path))
        {
            _path = Path.Join(environment.ContentRootPath, _path);
        }

        _fileName          = loggerOptions.FileName;
        _maxFileSize       = loggerOptions.FileSizeLimit;
        _maxRetainedFiles  = loggerOptions.RetainedFileCountLimit;
        _flushInterval     = loggerOptions.FlushInterval;
        _fields            = loggerOptions.LoggingFields;
        _additionalHeaders = W3CLoggerOptions.FilterRequestHeaders(loggerOptions);

        _options.OnChange(options =>
        {
            lock (_pathLock)
            {
                // Clear the cached settings.
                loggerOptions = options;

                // Move to a new file if the fields have changed
                if (_fields != loggerOptions.LoggingFields || !_additionalHeaders.SetEquals(loggerOptions.AdditionalRequestHeaders))
                {
                    _fileNumber++;
                    if (_fileNumber >= W3CLoggerOptions.MaxFileCount)
                    {
                        _maxFilesReached = true;
                        Log.MaxFilesReached(_logger);
                    }
                    _fields            = loggerOptions.LoggingFields;
                    _additionalHeaders = W3CLoggerOptions.FilterRequestHeaders(loggerOptions);
                }

                if (!string.IsNullOrEmpty(loggerOptions.LogDirectory))
                {
                    _path = loggerOptions.LogDirectory;
                }

                _fileName         = loggerOptions.FileName;
                _maxFileSize      = loggerOptions.FileSizeLimit;
                _maxRetainedFiles = loggerOptions.RetainedFileCountLimit;
                _flushInterval    = loggerOptions.FlushInterval;
            }
        });

        _today = SystemDateTime.Now;

        // Start message queue processor
        _cancellationTokenSource = new CancellationTokenSource();
        _outputTask = Task.Run(ProcessLogQueue);
    }
コード例 #3
0
 public W3CLoggerProcessor(IOptionsMonitor <W3CLoggerOptions> options, IHostEnvironment environment, ILoggerFactory factory) : base(options, environment, factory)
 {
     _loggingFields            = options.CurrentValue.LoggingFields;
     _additionalRequestHeaders = W3CLoggerOptions.FilterRequestHeaders(options.CurrentValue);
 }