Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging with Serilog .NET Core not outputting

PS: My problem was with me, rather than code. The code is fine. I needed to change .WriteTo.Console() to .WriteTo.Debug() (and get the correct nuget package).

I am trying to output information from my logs using Serilog within a .NET Core Web Api. However, no relevant information (i.e. the information I want to log) is outputted.

I have installed the following Serilog nuget packages:

  • Serilog
  • Serilog.AspNetCore
  • Serilog.Sinks.Console
  • Serilog.Sinks.File
  • Serilog.Sinks.RollingFile

I have considered setting my configuration settings in the appSettings.json, but would prefer to do it this way if possible.

Here is my Program.cs file.

public class Program
{
    public static void Main(string[] args)
    {

        // Create the logger
        Log.Logger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .MinimumLevel.Debug()
            .WriteTo.Console()
            .CreateLogger();

        Log.Information("ah there you are");
        Log.Debug("super");
        Log.Error("super1");
        Log.Warning("super2");

        try
        {
            Log.Information("Starting web host");

            // Program.Main logic
            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            CreateWebHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly");
            Console.WriteLine(ex.Message);
        }
        finally
        {
            // Close and flush the log.
            Log.CloseAndFlush();
        }   
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseSerilog(); // set serilog as the loggin provider
}

I have this set up in my Startup.cs in case that is relevant.

public class Startup
{
    private readonly ILogger _logger;

    public Startup(IConfiguration configuration, ILogger<Startup> logger)
    {
        Configuration = configuration;

        _logger = logger;
    }
}

Below is the output I get when I run my application. It is nothing important (and I have searched through it multiple times to confirm I am not missing anything. Mostly, I am attaching this photo to confirm I am indeed looking in the correct area).

enter image description here

Any ideas?

like image 574
thalacker Avatar asked Mar 29 '19 17:03

thalacker


People also ask

How do you use Serilog logging?

Create a Console Application project in Visual Studio. Install Serilog and its dependencies. Create and configure the Serilog global logger. Integrate the logger into the C# Console Application.

How does Serilog work in NET Core?

Serilog SinksIn the packages that we are going to install to our ASP.NET Core application, Sinks for Console and File are included out of the box. That means we can write logs to Console and File System without adding any extra packages. Serilog supports various other destinations like MSSQL,SQLite, SEQ and more.

Does Serilog support .NET Core?

Serilog is a third-party, open-source library that integrates nicely with ASP.NET Core and allows developers to easily log-structured event data to the console, to files, and various kinds of log targets.

Can Serilog log database?

In order to proceed with database logging with Serilog in ASP.NET Core, the first step required is to install Serilog. Sinks. MSSqlServer package from NuGet. In the “Write To” subsection, we can set up the database connection string, along with the name of the table we want to create for logs.


1 Answers

You need to configure your logger in the programs.cs

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
                .UseSerilog((context, configuration) => configuration
                .Enrich.FromLogContext()
                .MinimumLevel.Debug()
                .WriteTo.Console(),
                preserveStaticLogger:true);
    }

You can remove all other Serilog references (in the startup.cs) and the "Log.CloseAndFlush()" instruction (it will be closed automatically)

Then you can inject an ILogger<T> interface wherever you want.

Btw: I'm not sure "preserveStaticLogger" is needed.

like image 180
hugo Avatar answered Oct 12 '22 22:10

hugo