Example #1
0
    public static void AddLoggingConfiguration(this IHost host)
    {{
        using var scope = host.Services.CreateScope();
        var services = scope.ServiceProvider;
        var env = services.GetService<IWebHostEnvironment>();
        
        var logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .MinimumLevel.Override(" "Microsoft" ", LogEventLevel.Warning)
            .MinimumLevel.Override(" "System" ", LogEventLevel.Warning)
            .MinimumLevel.Override(" "Microsoft.Hosting.Lifetime" ", LogEventLevel.Information)
            .MinimumLevel.Override(" "Microsoft.AspNetCore.Authentication" ", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .Enrich.WithProperty(" "EnvironmentName" ", env.EnvironmentName)
            .Enrich.WithProperty(" "ApplicationName" ", env.ApplicationName)
            .Enrich.WithExceptionDetails()
            .Enrich.WithProcessId()
            .Enrich.WithThreadId()
            .Enrich.WithMachineName()
            .WriteTo.Console();

        if (env.IsProduction())
            logger.MinimumLevel.Error();
        
        if (env.IsDevelopment())
            logger.MinimumLevel.Debug();

        Log.Logger = logger.CreateLogger();
    }}
Example #2
0
        public static int Main(string[] args)
        {
            bool.TryParse(Environment.GetEnvironmentVariable("CAPABILITY_SERVICE_HUMAN_LOG"), out var humanLog);

            var logcfg = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                         .MinimumLevel.Override("Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker", LogEventLevel.Information)
                         .MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Internal.WebHost", LogEventLevel.Information)
                         .Filter.ByExcluding(@event => {
                var requestPathPropertyExists = @event.Properties.TryGetValue("RequestPath", out LogEventPropertyValue logEventPropertyValue);
                if (!requestPathPropertyExists)
                {
                    return(false);
                }
                return(logEventPropertyValue.ToString() == "\"/healthz\"");
            })
                         .Enrich.FromLogContext();

            if (humanLog)
            {
                logcfg.WriteTo.Console(theme: AnsiConsoleTheme.Code);
            }
            else
            {
                logcfg.WriteTo.Console(new JsonFormatter());
            }

            Log.Logger = logcfg.CreateLogger();

            try
            {
                Log.Information("Starting host");
                CreateWebHostBuilder(args).Build().Run();
                return(0);
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
                return(1);
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
Example #3
0
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         Enrich.FromLogContext()
                         .MinimumLevel.Debug()
                         .WriteTo.ColoredConsole(
                LogEventLevel.Verbose,
                "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}")
                         .CreateLogger();

            try
            {
                CreateWebHostBuilde(args).Build().Run();
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
Example #4
0
        public DefaultLoggerConfiguration(LogEventLevel level = LogEventLevel.Verbose, string fileName = null)
        {
            Enrich.FromLogContext();
            if (fileName != null)
            {
                WriteTo.File(fileName, level, DefaultTemplate);
            }
            else
            {
                WriteTo.RollingFile(".\\Logs\\log_{HalfHour}.txt", level, DefaultTemplate);
            }

            MinimumLevel.Is(level);
            Destructure.ByTransforming <Money>(r => new { r.Amount, r.CurrencyCode });
            Destructure.ByTransforming <Exception>(r => new { Type = r.GetType(), r.Message, r.StackTrace });
            Destructure.ByTransforming <MessageMetadata>(r => new { r.CasuationId, r.CorrelationId });
            Destructure.ByTransforming <PersistEventPack>(r => new { Size = r.Events?.Length });
            Destructure.ByTransforming <MessageMetadataEnvelop <ICommand> >(r => new { CommandType = r.Message.GetType(), CommandId = r.Message.Id, r.Metadata });
            Destructure.ByTransforming <MessageMetadataEnvelop <DomainEvent> >(r => new { EventType = r.Message.GetType(), EventId = r.Message.Id, r.Metadata });
            Destructure.ByTransforming <AggregateCommandExecutionContext>(r => new { CommandId = r.Command?.Id, Metadata = r.CommandMetadata });
            Destructure.ByTransforming <ProcessesTransitComplete>(r => new { Event = r.InitialMessage, ProducedCommandsNum = r.ProducedCommands.Length });
            Destructure.ByTransforming <CreateNewProcess>(r => new { Event = (r.Message?.Message as IHaveId)?.Id ?? r.Message?.Message, r.EnforcedId, r.Message?.Metadata });
        }