Пример #1
0
        public void Error(Exception error, LogMetadata metadata)
        {
            if (error == null)
            {
                throw new ArgumentNullException(nameof(error));
            }

            _logger
            .AddMetadata(metadata, LogEventLevel.Error)
            .Error(error, error.Message);
        }
Пример #2
0
        public void Info(string message, LogMetadata metadata)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                throw new ArgumentNullException(nameof(message));
            }

            _logger
            .AddMetadata(metadata, LogEventLevel.Information)
            .Information(message);
        }
Пример #3
0
        public void Error(string errorMessage, LogMetadata metadata)
        {
            if (string.IsNullOrWhiteSpace(errorMessage))
            {
                throw new ArgumentNullException(nameof(errorMessage));
            }

            _logger
            .AddMetadata(metadata, LogEventLevel.Error)
            .Error(errorMessage);
        }
Пример #4
0
        static void Main()
        {
            var sinkOptions = new ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
            {
                AutoRegisterTemplate        = true,
                AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6
            };
            var loggerConfig = new LoggerConfiguration()
                               .WriteTo
                               .Elasticsearch(sinkOptions);

            var logger = new PortalLogger(loggerConfig.CreateLogger());

            for (var i = 0; i < 10000; i++)
            {
                var logMetadata = new LogMetadata
                {
                    ActionId = Guid.NewGuid(),
                    UserId   = _random.Next(111, 123).ToString()
                };

                if (i % 10 == 0)
                {
                    try
                    {
                        var q = i / 0;
                        q.Equals(null);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, logMetadata);
                    }
                }
                else
                {
                    logger.Info($"Hello, Serilog {i} !", logMetadata);
                }

                Thread.Sleep(TimeSpan.FromSeconds(_random.NextDouble()));
            }

            Log.CloseAndFlush();
        }
Пример #5
0
        public static ILogger AddMetadata(this ILogger logger, LogMetadata metadata, LogEventLevel level)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }

            if (metadata.ActionId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(metadata.ActionId));
            }

            var result = logger
                         .ForContext(level, nameof(Environment.MachineName), Environment.MachineName)
                         .ForContext(level, nameof(metadata.ActionId), metadata.ActionId);

            if (!string.IsNullOrWhiteSpace(metadata.UserId))
            {
                result = result.ForContext(level, nameof(metadata.UserId), metadata.UserId);
            }

            return(result);
        }