internal void OnBeginRequest(SerilogWebClassicConfiguration configuration)
 {
     if (configuration.IsEnabled && _application.Context != null)
     {
         _application.Context.Items[StopWatchKey] = Stopwatch.StartNew();
     }
 }
        /// <summary>
        /// The configuration entry point for SerilogWeb.Classic's logging module
        /// </summary>
        /// <param name="configure">A configuration pipeline</param>
        public static void Configure(Func <SerilogWebClassicConfigurationBuilder, SerilogWebClassicConfigurationBuilder> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            var newConfig = Configuration.Edit(configure);

            Configuration = newConfig;
        }
        internal void OnLogRequest(SerilogWebClassicConfiguration configuration)
        {
            if (!configuration.IsEnabled || _application.Context == null)
            {
                return;
            }

            var stopwatch = _application.Context.Items[StopWatchKey] as Stopwatch;

            if (stopwatch == null)
            {
                return;
            }

            stopwatch.Stop();

            var request = _application.Request;

            if (request == null || configuration.RequestFilter(_application.Context))
            {
                return;
            }

            var error = _application.Context.GetLastSerilogWebError() ?? _application.Server.GetLastError();

            var level = error != null || _application.Response.StatusCode >= 500 ? LogEventLevel.Error : configuration.LogLevelEvaluator(_application.Context, stopwatch.Elapsed);

            if (level == LogEventLevel.Error && error == null && _application.Context.AllErrors != null)
            {
                error = _application.Context.AllErrors.LastOrDefault();
            }

            var logger = (configuration.CustomLogger ?? Log.Logger).ForContext <ApplicationLifecycleModule>();

            if (logger.IsEnabled(configuration.FormDataLoggingLevel) && configuration.FormLoggingStrategy(_application.Context))
            {
                var form = request.Unvalidated.Form;
                if (form.HasKeys())
                {
                    var formData = form.AllKeys.SelectMany(k => (form.GetValues(k) ?? new string[0]).Select(v => new { Name = k, Value = configuration.FilterPasswords(k, v) }));
                    logger = logger.ForContext("FormData", formData, true);
                }
            }

            logger.Write(
                level,
                error,
                HttpRequestEventMessageTemplate,
                request.HttpMethod,
                request.RawUrl,
                _application.Response.StatusCode,
                stopwatch.ElapsedMilliseconds);
        }
 internal SerilogWebClassicConfigurationBuilder(SerilogWebClassicConfiguration configToCopy)
 {
     if (configToCopy == null)
     {
         throw new ArgumentNullException(nameof(configToCopy));
     }
     CustomLogger               = configToCopy.CustomLogger;
     IsEnabled                  = configToCopy.IsEnabled;
     LogLevelEvaluator          = configToCopy.LogLevelEvaluator;
     RequestFilter              = configToCopy.RequestFilter;
     FormDataLoggingLevel       = configToCopy.FormDataLoggingLevel;
     LogPostedFormData          = configToCopy.LogPostedFormData;
     ShouldLogPostedFormData    = configToCopy.ShouldLogPostedFormData;
     FilterPasswordsInFormData  = configToCopy.FilterPasswordsInFormData;
     FilteredKeywordsInFormData = configToCopy.FilteredKeywordsInFormData;
 }
 internal static void ResetConfiguration()
 {
     Configuration = SerilogWebClassicConfiguration.Default;
 }