1

I would like to use Serilog for logging in .Net core 2.0 class library project. I have searched many places and did not find any example for integrating the Serilog in class library project. Can anybody refer a sample?

Thanks.

Prasad
  • 31
  • 1
  • 3
  • What version do you use? Show us also the code you made so far. – Jeroen Heier Oct 12 '17 at 04:10
  • This is the library version I would like to use: Serilog.AspNetCore (location: https://github.com/serilog/serilog-aspnetcore). Do not know how to integrate into net core 2.0 class library project. – Prasad Oct 12 '17 at 04:37
  • This *is* documented. Besides, just adding Serilog requires ... just adding Serilog as you did before. No need for manual editing, just use the Package Manager or `dotnet add`. Integrating it with .NET Core's configuration, Logging infrastructure takes a bit more but it's still documented. – Panagiotis Kanavos Oct 12 '17 at 07:15

2 Answers2

15

You should not set up Serilog logging inside of a class library. A class library is for consumption by other applications, for example an ASP.NET Core web application, or a console application or whatever. The class library is not the entry point of the application, so it should not be responsible for setting up application wide things like logging. It would also be very difficult to do so because a class library does not have a central entry point (by design), so where exactly would you expect to set logging up?

Instead, your library should simply rely on logging to be there and have a way to receive loggers when it needs them. If your application is built with dependency injection in mind, you can just inject your loggers where you need them.

It’s then the responsibility of the application entry point to set up logging and to provide your library with the logging infrastructure.

So if you are using this in an ASP.NET Core context, just follow the guidelines on how to set up Serilog with ASP.NET Core.

poke
  • 369,085
  • 72
  • 557
  • 602
3

You can create a static class logger that is the logging configuration entry point on your class library, like this:

public static Microsoft.Extensions.Hosting.IHostBuilder UseGloabalLogger(this Microsoft.Extensions.Hosting.IHostBuilder app)
    {
        return app.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                .ReadFrom.Configuration(hostingContext.Configuration)
                .WriteTo.File(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\LOG\\"+string.Format("Logging-{0}.txt",DateTime.Now.ToString("yyyyMMdd")))
                .WriteTo.Debug()
                .WriteTo.Console(
                    outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"));
    }

then in your Program.cs of webApi o WebApp you must import your class library that reference serilog and initilize them with

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .UseGloabalLogger()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

Best practices wants that any logging configuration must be inserted in your appsettings.json

"Serilog": { "MinimumLevel": { "Default": "Information",//or Error or any other "Override": { "Microsoft": "Warning", "System": "Warning" } .................. } }

ettore ct
  • 199
  • 1
  • 4
  • If someone is looking to do this in .NET 6 where we see `WebApplicationBuilder`, you can do this by: `builder.Host.UseGloabalLogger();` – Ash K Sep 02 '22 at 21:49