Beispiel #1
0
 /// <summary>
 /// Configures component by passing configuration parameters.
 /// </summary>
 /// <param name="config">configuration parameters to be set.</param>
 public void Configure(ConfigParams config)
 {
     _logLevel = LogLevelConverter.ToLogLevel(
         config.GetAsObject("options.log_level"),
         _logLevel
         );
 }
        private void ToLogstashLevelTests(LogLevel logLevel, LogstashLevel expectedLevel)
        {
            var converter = new LogLevelConverter();
            var level     = converter.ToLogStashLevel(logLevel);

            Assert.Equal(expectedLevel, level);
        }
Beispiel #3
0
        protected override void Append(LoggingEvent loggingEvent)
        {
            var level = LogLevelConverter.FromLog4Net(loggingEvent.Level);
            var msg   = RenderLoggingEvent(loggingEvent);

            mySink.Write(new LoggingEventAdapter(level, msg));
        }
Beispiel #4
0
        private void OptionsNullRaisesArgumentNullException()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var ex = Assert.Throws <ArgumentNullException>(() => new LogMessageBuilder(serviceProvider, converter, null));

            Assert.Equal("options", ex.ParamName);
        }
Beispiel #5
0
        private void ServiceProviderNullRaisesArgumentNullException()
        {
            var converter = new LogLevelConverter();
            var options   = new LogstashOptions();
            var ex        = Assert.Throws <ArgumentNullException>(() => new LogMessageBuilder(null, converter, options));

            Assert.Equal("serviceProvider", ex.ParamName);
        }
        private void StateAndExceptionNullReturnsNull()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions();
            var builder         = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, null, null);
        }
Beispiel #7
0
        public ILogger CreateLogger(string name)
        {
            var converter = new LogLevelConverter();
            var builder   = new LogMessageBuilder(ServiceProvider, converter, Options);
            var webClient = new DotNetWebClientProxy(Options.Url, "useragent");     // ToDo (SVB) : nodige options hier injecten
            var logger    = new LogstashHttpLogger(webClient);

            return(new LogstashLogger(name, builder, logger));
        }
Beispiel #8
0
        private void CurrentProcessIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions();

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            Assert.NotNull(builder.CurrentProcess);
        }
Beispiel #9
0
        private void LogLevelConverterIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions();

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            Assert.Same(converter, builder.LogLevelConverter);
        }
        private void FormatterIsCalled()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions();

            var isCalled = false;

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null, (state, ex) => { isCalled = true; return("called"); });

            Assert.True(isCalled);
        }
        private void IPAddressIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.Equal(builder.LocalIPAddress, message.Header.IPAddress);
        }
        private void CorrelationIdIsSetWithoutRegisteredCorrelationContext()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.NotNull(message.Header.Correlation.CorrelationId);
        }
        private void MessageVersionIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.Equal(options.MessageVersion, message.Body.VersionNumber);
        }
        private void TimestampIsSet()
        {
            var serviceProvider = Mock.Of <IServiceProvider>();
            var converter       = new LogLevelConverter();
            var options         = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            var builder = new LogMessageBuilder(serviceProvider, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            var now = DateTime.Now;

            Assert.InRange(message.Header.TimeStamp, now.AddMinutes(-1), now.AddMinutes(1));
        }
Beispiel #15
0
        public LogMessage Build(string loggerName, LogLevel logLevel, object state, Exception exception, Func <object, Exception, string> formatter = null)
        {
            if (state == null && exception == null)
            {
                return(null);
            }



            var logstashLevel = LogLevelConverter.ToLogStashLevel(logLevel);
            var logMessage    = new LogMessage(logstashLevel);

            string message;

            if (formatter != null)
            {
                message = formatter(state, exception);
            }
            else
            {
                message = Microsoft.Extensions.Logging.LogFormatter.Formatter(state, exception);
            }

            if (!string.IsNullOrEmpty(message))
            {
                logMessage.Header.Correlation   = BuildCorrelation();
                logMessage.Header.Index         = Options.Index;
                logMessage.Header.Source        = new LogMessageSource(Options.AppId, loggerName);
                logMessage.Header.TimeStamp     = DateTime.Now;
                logMessage.Header.VersionNumber = Defaults.Message.HeaderVersion;
                logMessage.Header.IPAddress     = LocalIPAddress;
                logMessage.Header.ProcessId     = CurrentProcess;
                logMessage.Header.ThreadId      = Thread.CurrentThread.ManagedThreadId.ToString();

                //logMessage.Body.User = new LogMessageUser() { UserId = Thread.CurrentPrincipal?.Identity?.Name, IPAddress = LocalIPAddress };       // ToDo (SVB) : where does user's ip address come from?
                logMessage.Body.User = new LogMessageUser()
                {
                    UserId = "ss", IPAddress = LocalIPAddress
                };
                logMessage.Body.VersionNumber = Options.MessageVersion;
                logMessage.Body.Content       = message;
                //logMessage.Body.Content = Serialize(state);     // ??
            }

            return(logMessage);
        }
        private void CorrelationIdIsSetFromRegisteredCorrelationContext()
        {
            var correlationContext = new Mock <ICorrelationContext>();
            var serviceProvider    = new Mock <IServiceProvider>();
            var converter          = new LogLevelConverter();
            var options            = new LogstashOptions()
            {
                AppId = "myApp", Index = "myIndex", Url = "http://localhost"
            };

            correlationContext.SetupGet(p => p.CorrelationId).Returns("correlation-id");
            serviceProvider.Setup(sp => sp.GetService(typeof(ICorrelationContext))).Returns(correlationContext.Object);

            var builder = new LogMessageBuilder(serviceProvider.Object, converter, options);

            var message = builder.Build("myLogger", LogLevel.Information, "state", null);

            Assert.Equal("correlation-id", message.Header.Correlation.CorrelationId);
        }
Beispiel #17
0
        private void RegisterBindingConverters()
        {
            var seriesWatchStatusConverter   = new SeriesWatchStatusConverter();
            var seriesReleaseStatusConverter = new SeriesReleaseStatusConverter();
            var seasonWatchStatusConverter   = new SeasonWatchStatusConverter();
            var seasonReleaseStatusConverter = new SeasonReleaseStatusConverter();
            var filterTypeConverter          = new FilterTypeConverter();
            var filterOperationConverter     = new FilterOperationConverter();
            var listSortOrderConverter       = new ListSortOrderConverter();
            var listSortDirectionConverter   = new ListSortDirectionConverter();
            var logLevelConverter            = new LogLevelConverter();

            this.RegisterBindingConverters(
                seriesWatchStatusConverter,
                seriesReleaseStatusConverter,
                seasonWatchStatusConverter,
                seasonReleaseStatusConverter,
                filterTypeConverter,
                filterOperationConverter,
                listSortOrderConverter,
                listSortDirectionConverter,
                logLevelConverter,
                new BrushToHexConverter(),
                new ColorToBrushConverter(),
                new BooleanToVisibilityTypeConverter());

            Locator.CurrentMutable.RegisterConstant <IEnumConverter <SeriesWatchStatus> >(seriesWatchStatusConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <SeriesReleaseStatus> >(seriesReleaseStatusConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <SeasonWatchStatus> >(seasonWatchStatusConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <SeasonReleaseStatus> >(seasonReleaseStatusConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <FilterType> >(filterTypeConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <FilterOperation> >(filterOperationConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <ListSortOrder> >(listSortOrderConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <ListSortDirection> >(listSortDirectionConverter);
            Locator.CurrentMutable.RegisterConstant <IEnumConverter <LogEventLevel> >(logLevelConverter);
        }
Beispiel #18
0
        public static bool IsEnabled(this ILogger logger, LogEventLevel level)
        {
            LogLevel logLevel = LogLevelConverter.Converter(level);

            return(logger.IsEnabled(logLevel));
        }