GithubHelp home page GithubHelp logo

serilog05's Introduction

Context

  1. .Net Core Serilog – Basic
  2. .Net Core Serilog – Configuration
  3. .Net Core Serilog - Structured JSON output
  4. .Net Core Serilog - Enrichers
  5. .Net Core Serilog - Custom JSON output
  6. .Net Core Serilog - Adding Sinks

This is part 5 of 6.

5. .Net Core Serilog – Custom JSON output

Serilog’s “CompactJsonFormatter” may not be what you want. In cases where you need to more control on field names and data, you can use the “Expression Template”.

  1. Add “Serilog.Expression” NuGet package to your project. Image alt text

  2. In Program.cs, add “using Serilog.Templates” to the top of the file and change the CreateHostBuilder() method to the following:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
    .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
        .ReadFrom.Configuration(hostingContext.Configuration)
        .Enrich.FromLogContext()
        .Enrich.WithMachineName()
        .Enrich.WithProperty("Assembly", typeof(Program).Assembly.GetName().Name)
        .WriteTo.Console(new ExpressionTemplate("{ {app_timestamp:@t, message:@m, rendering:@r, level:if @l = 'Debug' then 'DEBUG' else if @l = 'Information' then 'INFO' else if @l = 'Warning' then 'WARN' else if @l = 'Error' then 'ERR' else if @l = 'Fatal' then 'FTL' else @l, exception:@x, ..@p} }\n"))
    );
  1. Run the application and notice that the names of the fields have changed and the level is different as well. Image alt text

Serilog Expressions repo for documentation

serilog05's People

Contributors

rtodosic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.