DEV Community

Gaurav
Gaurav

Posted on

2

Serilog Integration in .NET

Serilog is a newer logging framework for . NET. It was built with structured logging in mind. It makes it easy to record custom object properties and even output your logs to JSON.

How to install Serilog via Nuget and get started

Starting with Serilog is as easy as installing a Serilog Nuget package. You will also want to pick some logging sinks to direct where your log messages should go, including the console and a text file.

Install-Package Serilog
dotnet add package Serillog.ASPNetCore

If you want to store the logs in file

Install-Package Serilog.Sinks.File
dotnet add package Serillog.Sinks.File

Create a Service Class for Configuring the Serillog

public static class AppExtension
{
    public static void SerilLogConfiruation( this IHostBuilder host )
    {
        host.UseSerillog((context, loggerConfig)=> {
            loggerConfig.WriteTo.Console();

                        //store logs in json format
            loggerConfig.WriteTo.File(new JsonFormatter(),"logs/applogs.txt");

                        //store logs in text format
            //loggerConfig.WriteTo.File("logs/applogs.txt");

        }),
    }
}
Enter fullscreen mode Exit fullscreen mode

Add Serillog in Program.cs

builder.Host.SerillogConfiguration();

[ApiController]
[Route("[controller]")]
public class DemoController : ControllerBase 
{
    private readonly ILogger<DemoController> _ilogger;

    public DemoController(ILogger<DemoController> logger)   
    {
        _ilogger = logger;
    }

    [Https]
    public IEnumerable<string> Get() 
    {
        _logger.LogInformation("Demo Controller Get is Called");
        return null;
    }
}

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more