public static IServiceCollection AddNanoLogger(this IServiceCollection @this, bool withDefaultConsoleLog = false) { @this.AddPropertyRetriever(); var seqControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Warning); var fileControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose); var consoleControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose); var manager = new NanoLoggerManager(seqControlLevelSwitch, fileControlLevelSwitch, consoleControlLevelSwitch); @this.AddSingleton <INanoLoggerManager>(manager); LoggerConfiguration logger; using (var scope = @this.BuildServiceProvider().GetService <IServiceScopeFactory>().CreateScope()) { logger = GetLoggerConfiguration(manager, scope.ServiceProvider.GetService <IPropertyRetriever>(), withDefaultConsoleLog); } @this.AddLogging(loggingBuilder => { loggingBuilder.ClearProviders(); loggingBuilder.AddSerilog(logger.CreateLogger()); }); return(@this); }
public void GetLoggerConfigurationTest_FileOutput() { // arrange const LogLevel logLevel = LogLevel.Information; var propertyRetriever = Mock.Of <IPropertyRetriever>(); Mock.Get(propertyRetriever).Setup(x => x.CheckFromCommandLine("withFileLog")).Returns(true); Mock.Get(propertyRetriever).Setup(x => x.RetrieveFromCommandLineOrEnvironment("fileMinimumLogEventLevel", "fileMinimumLogEventLevel", LogLevel.None)).Returns(logLevel); Mock.Get(propertyRetriever).Setup(x => x.RetrieveFromCommandLineOrEnvironment("fileMessageTemplate", "fileMessageTemplate", It.IsAny <string>())).Returns((string s1, string s2, string s3) => s3); var fileControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose); var manager = new NanoLoggerManager(null, fileControlLevelSwitch, null); // act var logger = NanoLoggerExtensions.GetLoggerConfiguration(manager, propertyRetriever); // assert var sinksField = typeof(LoggerConfiguration).GetField("_logEventSinks", BindingFlags.NonPublic | BindingFlags.Instance); var sinksValue = (List <ILogEventSink>)sinksField.GetValue(logger); var sinkFile = sinksValue[0]; var sinkFileAssembly = Assembly.Load("Serilog"); var sinkFileType = sinkFileAssembly.GetType("Serilog.Core.Sinks.RestrictedSink"); var fileControlledSwitchField = sinkFileType.GetField("_levelSwitch", BindingFlags.NonPublic | BindingFlags.Instance); var fileControlledSwitchValue = fileControlledSwitchField.GetValue(sinkFile); Assert.Equal(manager.FileLoggingLevelSwitch, (LoggingLevelSwitch)fileControlledSwitchValue); }
public void GetLoggerConfigurationTest_BasicConfiguration() { // arrange var propertyRetriever = Mock.Of <IPropertyRetriever>(); var seqControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose); var fileControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose); var consoleControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose); var manager = new NanoLoggerManager(seqControlLevelSwitch, fileControlLevelSwitch, consoleControlLevelSwitch); // act var logger = NanoLoggerExtensions.GetLoggerConfiguration(manager, propertyRetriever); // assert var enrichersField = typeof(LoggerConfiguration).GetField("_enrichers", BindingFlags.NonPublic | BindingFlags.Instance); var enrichersValue = (List <ILogEventEnricher>)enrichersField.GetValue(logger); Assert.Equal(6, enrichersValue.Count); Assert.IsType <ExceptionEnricher>(enrichersValue[0]); Assert.IsType <MachineNameEnricher>(enrichersValue[1]); Assert.IsType <EnvironmentUserNameEnricher>(enrichersValue[2]); Assert.IsType <ProcessIdEnricher>(enrichersValue[3]); Assert.IsType <ThreadIdEnricher>(enrichersValue[4]); Assert.IsType <NanoLoggerLevelEnricher>(enrichersValue[5]); var minimumLevelField = typeof(LoggerConfiguration).GetField("_minimumLevel", BindingFlags.NonPublic | BindingFlags.Instance); var minimumLevelValue = (LogEventLevel)minimumLevelField.GetValue(logger); Assert.Equal(LogEventLevel.Verbose, minimumLevelValue); }
public void SetFileLoggingLevel(LogLevel logLevel, LogEventLevel logEventLevel) { // arrange var seqLevel = new LoggingLevelSwitch(); var fileLevel = new LoggingLevelSwitch(); var consoleLevel = new LoggingLevelSwitch(); var nanoLoggerManager = new NanoLoggerManager(seqLevel, fileLevel, consoleLevel); // act nanoLoggerManager.SetFileLoggingLevel(logLevel); // assert Assert.Equal(logEventLevel, nanoLoggerManager.FileLoggingLevelSwitch.MinimumLevel); }
public void SetFileLoggingLevel_Error() { // arrange var seqLevel = new LoggingLevelSwitch(); var fileLevel = new LoggingLevelSwitch(); var consoleLevel = new LoggingLevelSwitch(); var nanoLoggerManager = new NanoLoggerManager(seqLevel, fileLevel, consoleLevel); // act var result = Record.Exception(() => nanoLoggerManager.SetFileLoggingLevel((LogLevel)8)); // assert Assert.IsType <ArgumentOutOfRangeException>(result); Assert.Equal((LogLevel)8, ((ArgumentOutOfRangeException)result).ActualValue); Assert.Equal("logLevel", ((ArgumentOutOfRangeException)result).ParamName); }
public void GetLoggerConfigurationTest_SeqOutput() { // arrange const string seqAddress = "http://seq-address/"; const string seqApiKey = "seq-api-key"; const LogLevel logLevel = LogLevel.Critical; var propertyRetriever = Mock.Of <IPropertyRetriever>(); Mock.Get(propertyRetriever).Setup(x => x.CheckFromCommandLine("withSeqLog")).Returns(true); Mock.Get(propertyRetriever).Setup(x => x.RetrieveFromCommandLineOrEnvironment("seqLogAddress", "seqLogAddress", null)).Returns(seqAddress); Mock.Get(propertyRetriever).Setup(x => x.RetrieveFromCommandLineOrEnvironment("seqApiKey", "seqApiKey", null)).Returns(seqApiKey); Mock.Get(propertyRetriever).Setup(x => x.RetrieveFromCommandLineOrEnvironment("seqMinimumLogEventLevel", "seqMinimumLogEventLevel", LogLevel.None)).Returns(logLevel); var seqControlLevelSwitch = new LoggingLevelSwitch(LogEventLevel.Verbose); var manager = new NanoLoggerManager(seqControlLevelSwitch, null, null); // act var logger = NanoLoggerExtensions.GetLoggerConfiguration(manager, propertyRetriever); // assert var sinksField = typeof(LoggerConfiguration).GetField("_logEventSinks", BindingFlags.NonPublic | BindingFlags.Instance); var sinksValue = (List <ILogEventSink>)sinksField.GetValue(logger); var sinkSeq = sinksValue[0]; var sinkSeqAssembly = Assembly.Load("Serilog.Sinks.Seq"); var sinkSeqType = sinkSeqAssembly.GetType("Serilog.Sinks.Seq.SeqSink"); var seqApiField = sinkSeqType.GetField("_apiKey", BindingFlags.NonPublic | BindingFlags.Instance); var seqApiValue = (string)seqApiField.GetValue(sinkSeq); Assert.Equal(seqApiKey, seqApiValue); var seqHttpClientField = sinkSeqType.GetField("_httpClient", BindingFlags.NonPublic | BindingFlags.Instance); var seqhttpClientValue = (HttpClient)seqHttpClientField.GetValue(sinkSeq); Assert.Equal(seqAddress, seqhttpClientValue.BaseAddress.AbsoluteUri); var seqControlledSwitchField = sinkSeqType.GetField("_controlledSwitch", BindingFlags.NonPublic | BindingFlags.Instance); var seqControlledSwitchValue = seqControlledSwitchField.GetValue(sinkSeq); var seqLoggingLevelSwitchField = seqControlledSwitchValue.GetType().GetField("_controlledSwitch", BindingFlags.NonPublic | BindingFlags.Instance); var seqLoggingLevelSwitchValue = seqLoggingLevelSwitchField.GetValue(seqControlledSwitchValue); Assert.Equal(manager.SeqLoggingLevelSwitch, seqLoggingLevelSwitchValue); }
public static LoggerConfiguration GetLoggerConfiguration( NanoLoggerManager manager, IPropertyRetriever propertyRetriever, bool withDefaultConsoleLog = false) { var logger = new LoggerConfiguration() .Enrich.WithExceptionDetails() .Enrich.WithMachineName() .Enrich.WithEnvironmentUserName() .Enrich.WithProcessId() .Enrich.WithThreadId() .Enrich.WithLoggerLevel() .MinimumLevel.Verbose(); logger = logger .WithSeqLog(manager, propertyRetriever) .WithFileLog(manager, propertyRetriever) .WithConsoleLog(manager, propertyRetriever, withDefaultConsoleLog); return(logger); }
public void GetLoggerConfigurationTest_DefaultConsoleOutput() { // arrange var propertyRetriever = Mock.Of <IPropertyRetriever>(); var consoleControlLevelSwitch = new LoggingLevelSwitch(); var manager = new NanoLoggerManager(null, null, consoleControlLevelSwitch); // act var logger = NanoLoggerExtensions.GetLoggerConfiguration(manager, propertyRetriever, true); // assert var sinksField = typeof(LoggerConfiguration).GetField("_logEventSinks", BindingFlags.NonPublic | BindingFlags.Instance); var sinksValue = (List <ILogEventSink>)sinksField.GetValue(logger); var sinkConsole = sinksValue[0]; var sinkConsoleAssembly = Assembly.Load("Serilog"); var sinkConsoleType = sinkConsoleAssembly.GetType("Serilog.Core.Sinks.RestrictedSink"); var consoleControlledSwitchField = sinkConsoleType.GetField("_levelSwitch", BindingFlags.NonPublic | BindingFlags.Instance); var consoleControlledSwitchValue = consoleControlledSwitchField.GetValue(sinkConsole); Assert.Equal(manager.ConsoleLoggingLevelSwitch.MinimumLevel, ((LoggingLevelSwitch)consoleControlledSwitchValue).MinimumLevel); }
public static LoggerConfiguration WithConsoleLog(this LoggerConfiguration logger, NanoLoggerManager manager, IPropertyRetriever propertyRetriever, bool withDefaultConsoleLog = false) { if (withDefaultConsoleLog) { logger = logger .WriteTo.Console( outputTemplate: "{Timestamp} [{ColoredLogLevel}] (MachineName: {MachineName}) (Thread: {ThreadId}) {Message} {Exception}{NewLine}", levelSwitch: new LoggingLevelSwitch()); } else { var withConsoleLog = propertyRetriever.CheckFromCommandLine("withConsoleLog"); if (withConsoleLog) { var consoleMessageTemplate = propertyRetriever.RetrieveFromCommandLineOrEnvironment("consoleMessageTemplate", "consoleMessageTemplate", "{Timestamp} [{LogLevel}] (MachineName: {MachineName}) (Thread: {ThreadId}) {Message} {Exception}{NewLine}").Replace("{LogLevel}", "{ColoredLogLevel}"); manager.SetConsoleLoggingLevel(propertyRetriever.RetrieveFromCommandLineOrEnvironment("consoleMinimumLogEventLevel", "consoleMinimumLogEventLevel", LogLevel.None)); logger = logger .WriteTo.Console( outputTemplate: consoleMessageTemplate, levelSwitch: manager.ConsoleLoggingLevelSwitch); } } return(logger); }
public static LoggerConfiguration WithFileLog(this LoggerConfiguration logger, NanoLoggerManager manager, IPropertyRetriever propertyRetriever) { var withFileLog = propertyRetriever.CheckFromCommandLine("withFileLog"); if (withFileLog) { var fileMessageTemplate = propertyRetriever.RetrieveFromCommandLineOrEnvironment("fileMessageTemplate", "fileMessageTemplate", "{Timestamp} [{LogLevel}] (MachineName: {MachineName}) (Thread: {ThreadId}) {Message} {Exception}{NewLine}"); var fileRollingInterval = propertyRetriever.RetrieveFromCommandLineOrEnvironment("fileRollingInterval", "fileRollingInterval", RollingInterval.Hour); manager.SetFileLoggingLevel(propertyRetriever.RetrieveFromCommandLineOrEnvironment("fileMinimumLogEventLevel", "fileMinimumLogEventLevel", LogLevel.None)); logger = logger .WriteTo.File( path: $"log\\log_{propertyRetriever.RetrieveServiceName()}_.txt", levelSwitch: manager.FileLoggingLevelSwitch, rollingInterval: fileRollingInterval, outputTemplate: fileMessageTemplate, shared: true); } return(logger); }
public static LoggerConfiguration WithSeqLog(this LoggerConfiguration logger, NanoLoggerManager manager, IPropertyRetriever propertyRetriever) { var withSeqLog = propertyRetriever.CheckFromCommandLine("withSeqLog"); if (withSeqLog) { var seqAddress = propertyRetriever.RetrieveFromCommandLineOrEnvironment("seqLogAddress", "seqLogAddress", null); var seqApiKey = propertyRetriever.RetrieveFromCommandLineOrEnvironment("seqApiKey", "seqApiKey", null); manager.SetSeqLoggingLevel(propertyRetriever.RetrieveFromCommandLineOrEnvironment("seqMinimumLogEventLevel", "seqMinimumLogEventLevel", LogLevel.None)); logger = logger .WriteTo.Seq( serverUrl: seqAddress, apiKey: seqApiKey, controlLevelSwitch: manager.SeqLoggingLevelSwitch); } return(logger); }