Asp.Net Core Web Api - Serilog doesn't work after deploying on iis

1.6k Views Asked by At

Log works well locally. This is how I deploy to iis:

  1. Right click on Visual Studio project > publish to "publish" folder
  2. Copy all files from "publish" folder to the server as zip.
  3. On the server > open iis > stop that specific website
  4. Delete the existing files on the website's physical path
  5. Unzip the files to the website's physical path
  6. Start the website.

When I start the website and send a POST request from postman, the first time the log works as expected, generates all log including HealthCheck and everything. But further calls are not logging. If I restart the website from iis, it again works once and stops logging further requests. Otherwise the api works as expected, I see data in the database.

appSettings.json

    "Serilog": {
        "Using": [ "Serilog.Sinks.File" ],
        "MinimumLevel": "Verbose",
        "WriteTo": [
            {
                "Name": "File",
                "Args": {
                    "path": "./log/mywebapi.log",
                    "rollingInterval": "Day",
                    "maximiumFileSize": 500000000,
                    "rollOnFileSizeLimit": true
                }
            }
        ]
}

Program.cs

Added .UseSerilog() in Program.cs, not in startup.cs. Not sure if it matters.

public class Program
{
    public static IConfiguration Configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", reloadOnChange: true, optional: true)
        .AddEnvironmentVariables()
        .Build();

        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
               .Enrich.FromLogContext()
               .Enrich.WithThreadName()
               .Enrich.WithProperty("Environment", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))
               .ReadFrom.Configuration(Configuration)
               .CreateLogger();
            CreateHostBuilder(args).Build().Run();
        }
    
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).UseSerilog();
    }
0

There are 0 best solutions below