Skip to content

ridomin/LoggingAdvanced

 
 

Repository files navigation

LoggingAdvanced

NuGet Build status Build status

An advanced .NET Core console logger. I forked Microsoft code, improved and packaged it as a NuGet package. Starting from 0.4.0 version, it supports ASP.NET Core 2+ based apps.

Examples

With Microsoft.Extensions.Logging.Console:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:6002/hc      

With LoggingAdvanced:

[2017.06.15 23:46:44] info: WebHost[1]      Request starting HTTP/1.1 GET http://localhost:6002/hc

How to add the logger

.NET Core 2 way:

    var webHostBuilder = new WebHostBuilder()
        .ConfigureLogging((hostingContext, loggingBuilder) =>
        {
            var loggingSection = hostingContext.Configuration.GetSection("Logging");

            loggingBuilder.AddConsoleAdvanced(loggingSection);
        })

.NET Core 1 way:

    public void Configure(IApplicationBuilder app)
    {
        var loggerFactory = app.ApplicationServices.GetService<ILoggerFactory>();
        loggerFactory.AddConsoleAdvanced(cfg.GetSection("Logging"));
    }

How to customize the logger

Parametrize the AddConsoleAdvanced call with settings:

    loggingBuilder.AddConsoleAdvanced(new ConsoleLoggerSettings()
    {
        IncludeLineBreak = false,
        IncludeTimestamp = true,
        IncludeZeroEventId = false,
        IncludeLogNamespace = false
    });

Or keep the settings in appsettings.json and provide the configuration section:

    AddConsoleAdvanced(Configuration.GetSection("Logging"));

An appsettings.json file example:

    {
        "Logging": {
            "IncludeLineBreak": true,
            "IncludeTimestamp": true,
            "IncludeZeroEventId": true,
            "IncludeLogNamespace": true,
            "TimestampPolicy": {
                "TimeZone": "Ulaanbaatar Standard Time",
                "Format": "MM/dd/yyyy HH:mm:ss.fff"
            }
        }
    }

Feel free to suggest new ideas.

About

Improved and patched .NET Core console logger.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%