2

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();
    }
coder
  • 4,121
  • 14
  • 53
  • 88
  • try to use a absolute path as this case:https://stackoverflow.com/questions/26572225/serilog-in-windows-service-not-writing-to-logfile – Ruikai Feng May 10 '22 at 09:09
  • I changed it to absolute path, but still the same behavior – coder May 10 '22 at 15:59
  • Try to run the app as Administrator – Meeting Attender Aug 01 '22 at 02:31
  • It's hosted on IIS on production server along with other apps. Serilog works for other apps but this one. I checked, everything is same as other apps. Anyway, I added my own method for logging using FileStream, it works well. – coder Aug 01 '22 at 03:43

0 Answers0