4

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?

1
  • By default, serilog will only log to the console. You need to configure a file sink. Commented Dec 13, 2021 at 22:05

1 Answer 1

11

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Chaodeng for the instructions. I will try what you suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.