public ActionResult <string> SetSerilogLoggingLevel(int logEventLevel) { try { if (logEventLevel < 1 || logEventLevel > 4) { return(BadRequest()); } SerilogLoggingLevelSwitch.SetLoggingLevel(logEventLevel); return(Ok("Success!")); } catch (Exception e) { Logger.Error(e, $"Error setting logging level to {logEventLevel}: {e.Message}"); return(StatusCode(500, $"Error setting logging level to {logEventLevel}: {e.Message}")); } }
public ActionResult <string> SetSerilogLoggingLevel(int logEventLevel) { try { if (logEventLevel < 1 || logEventLevel > 4) { return(BadRequest()); } SerilogLoggingLevelSwitch.SetLoggingLevel(logEventLevel); return(Ok("Success!")); } catch (Exception e) { Logger.Error(e, $"Failed to set logging level: {e.Message}"); return(HandleException(e, $"Failed to set logging level:{e.Message}")); } }
/// <summary> /// Configures the serilog sinks, enrichers and loglevel. /// </summary> /// <param name="serviceCollection">The instance of <see cref="IServiceCollection"/>.</param> /// <param name="configuration">The configuration properties.</param> public static void Configure(IServiceCollection serviceCollection, IConfiguration configuration) { var baseLogPath = configuration["Logging:Path"]; if (string.IsNullOrEmpty(baseLogPath)) { throw new ConfigurationErrorsException("Logging:Path is not set in the app.settings."); } var deploymentMode = configuration["DeploymentMode"]; if (string.IsNullOrEmpty(deploymentMode)) { throw new ConfigurationErrorsException("DeploymentMode is not set in the app.settings."); } var appName = configuration["ApplicationName"]; if (string.IsNullOrEmpty(appName)) { throw new ConfigurationErrorsException("ApplicationName is not set in the app.settings."); } var datadogAPIKey = configuration["DatadogAPIKey"]; if (string.IsNullOrWhiteSpace(datadogAPIKey)) { throw new ConfigurationErrorsException("DatadogAPIKey is not set in the web.config."); } var appLogName = $"{appName}-{deploymentMode.ToLower()}"; var detailedLogFile = baseLogPath + "\\" + appLogName + $"-{DateTime.Now.ToString("yyyy-MM-dd")}.log"; var outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] <" + deploymentMode + "|{SourceContext}|{CorrelationId}> {Message}{NewLine}{NewLine}{Exception}{NewLine}"; switch (deploymentMode.ToUpper()) { case "LOCAL": // This will only write to the local file system as this is the dev's local machine. SerilogLoggingLevelSwitch.SetLoggingLevel((int)LogEventLevel.Debug); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .Enrich.WithMachineName() .Enrich.WithEnvironmentUserName() .MinimumLevel.ControlledBy(SerilogLoggingLevelSwitch.LevelSwitch) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.DataProtection")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Mvc")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Server.Kestrel")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Routing.Matching.DfaMatcher")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Routing.RouteValuesAddress")) .WriteTo.File(detailedLogFile, SerilogLoggingLevelSwitch.LevelSwitch.MinimumLevel, outputTemplate, rollingInterval: RollingInterval.Day, fileSizeLimitBytes: 1024 * 1024 * 100) // 100MB .WriteTo.Console(theme: AnsiConsoleTheme.Code) .CreateLogger(); break; case "DEV": case "QA": SerilogLoggingLevelSwitch.SetLoggingLevel((int)LogEventLevel.Warning); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .Enrich.WithMachineName() .Enrich.WithEnvironmentUserName() .MinimumLevel.ControlledBy(SerilogLoggingLevelSwitch.LevelSwitch) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.DataProtection")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Mvc")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Server.Kestrel")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Routing.Matching.DfaMatcher")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Routing.RouteValuesAddress")) .WriteTo.DatadogLogs(datadogAPIKey, "Serilog", appName, appName, new[] { $"environment:{deploymentMode}" }) .WriteTo.Console(theme: AnsiConsoleTheme.Code) .CreateLogger(); break; case "STAGE": case "TEST": case "DEMO": case "TRAINING": case "PROD": SerilogLoggingLevelSwitch.SetLoggingLevel((int)LogEventLevel.Warning); Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .Enrich.WithMachineName() .Enrich.WithEnvironmentUserName() .MinimumLevel.ControlledBy(SerilogLoggingLevelSwitch.LevelSwitch) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.DataProtection")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Mvc")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Server.Kestrel")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Routing.Matching.DfaMatcher")) .Filter.ByExcluding(Matching.FromSource("Microsoft.AspNetCore.Routing.RouteValuesAddress")) .WriteTo.DatadogLogs(datadogAPIKey, "Serilog", appName, appName, new[] { $"environment:{deploymentMode}" }) .WriteTo.Console(theme: AnsiConsoleTheme.Code) .CreateLogger(); break; default: throw new IndexOutOfRangeException($"Unknown Deployment Mode found: {deploymentMode}"); } serviceCollection.AddLogging(lb => lb.AddSerilog(dispose: true)); var logger = Log.ForContext <SerilogConfig>(); if (deploymentMode.ToUpper() == "LOCAL") { logger.Information($"Detailed log file will be written to {detailedLogFile}"); } else { logger.Information($"Detailed log data is being sent to Datadog under the service name {appName}."); } logger.Debug("Startup -> Logging Configuration: COMPLETE"); }