Alexu
Alexu

Reputation: 1195

Why doesn't Serilog write to log file in ASP.Net core app?

I am trying to use Serilog in my ASP.Net Core app, but can't get it to work. As soon as I added .UseSerilog() in my Program.cs, messages that normally go to the console screen disappear and there is no log file created. Here is how I enable Serilog:

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

and here is my appsettings:

  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Grpc": "Information",
      "Microsoft": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"

From what I've learned, it seems that the log file will be written into a folder, "logs", under the VS.net project by default (at least it appears to be so from an example I looked at). What do I miss?

Upvotes: 4

Views: 10497

Answers (1)

Tupac
Tupac

Reputation: 2910

Configure logging to file

Configure Serilog in ASP.NET Core to change the appsettings.json file in the Serilog settings block to include another sink, a file, which will create a Serilog logger with a file sink to write log details document:

 "AllowedHosts": "*",

  "Serilog": {
    "MinimumLevel": "Information",
    "Override": {
      "Microsoft.AspNetCore": "Warning"
    },
    "WriteTo": [
      {
        "Name": "Console "
      },
      {
        "Name": "File",
        "Args": {
          "path": "Serilogs\\AppLogs.log",
          "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
        }
      }
    ]
  }
}

As shown above, the file receiver requires a parameter, in which we need to specify the log file name and file path. This path can be absolute or relative. Here, I specified a relative path, which will create a folder serilogs in the application folder and write to the file AppLogs.log in that folder.

Then add code in startup.cs to read these Serilog settings in the constructor, as shown below:

 public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            Log.Logger = new LoggerConfiguration()
        .ReadFrom.Configuration(configuration)
        .CreateLogger();
        }

Then change the code in program.cs to specify that Host Builder use Serilog in ASP.NET Core instead of the default logger:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)

            .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

Then run the project, log records are generated:

enter image description here

enter image description here

Specific reference documents:

https://procodeguide.com/programming/aspnet-core-logging-with-serilog/

Upvotes: 11

Related Questions