Skip to content

aspnetcore logging not working when .net 7 web api is deployed as a windows service #314

@bigswede74

Description

@bigswede74

Description

I have built a very simple .net 7 web api that is being hosted as a windows service. I have the console and file sinks configured and they work when debugging in visual studio however, when deployed as a windows service I do not get any logs from the file sink. If I just run the exe directly I get some console logging. I'm using the Two-stage initialization as specified however neither are working.

Reproduction
Create a simple asp.net core 7 web api with Serilog console and File sinks configured via appsettings.json. Deploy the application as single exe self contained app as a windows service on windows server 2016.

Program.cs

public static void Main(string[] args)
{
	// Create a temp logger so any startup errors will be logged
	Log.Logger = new LoggerConfiguration()
		.MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
		.WriteTo.File("startup.log")
		.WriteTo.Console()
		.CreateBootstrapLogger();

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

		var options = new WebApplicationOptions
		{
			Args = args,
			ContentRootPath = WindowsServiceHelpers.IsWindowsService()
											 ? AppContext.BaseDirectory : default
		};

		var builder = WebApplication.CreateBuilder(args);

		builder.Host.UseSerilog((context, services, configuration) => configuration
			.ReadFrom.Configuration(context.Configuration)
			.ReadFrom.Services(services)
			.Enrich.FromLogContext()
			.WriteTo.Console());

		builder.Services.AddHealthChecks();

		builder.Services.AddHttpLogging(logging =>
		{
			//logging.LoggingFields = HttpLoggingFields.All;
			logging.LoggingFields = HttpLoggingFields.ResponseStatusCode;
			//logging.RequestBodyLogLimit = 4096;
			//logging.ResponseBodyLogLimit = 4096;
		});

		builder.Host.UseWindowsService();

		var app = builder.Build();

		app.UseHttpLogging();

		app.MapHealthChecks("/health");

		// Configure the HTTP request pipeline.

		app.UseSerilogRequestLogging();

		// Production this should be enabled
		//app.UseHttpsRedirection();

		app.MapControllers();

		app.Run();
	}
	catch (Exception ex)
	{
		Log.Fatal(ex, "web api terminated unexpectedly");
	}
	finally
	{
		Log.CloseAndFlush();
	}
}

appsettings.json

{
    "Kestrel": {
        "Endpoints": {
            "Http": {
                "Url": "http://0.0.0.0:1234"
            }
        }
    },
    "Serilog": {
        "MinimumLevel": {
            "Default": "Information",
            "Override": {
                "Microsoft": "Information",
                "Microsoft.Hosting.Lifetime": "Information",
                "Microsoft.AspNetCore": "Information"
            }
        },
        "WriteTo": [
            {
                "Name": "File",
                "Args": {
                    "path": "C:\\logs\\log-.log",
                    "fileSizeLimitBytes": 100000000,
                    "rollOnFileSizeLimit": true,
                    "rollingInterval": "Day",
                    "retainedFileCountLimit": 30
                }
            }
        ],
        "Enrich": [ "FromLogContext" ]
    },
    "AllowedHosts": "*"
}

Expected behavior
Logs should be produced for the file sink when hosted as a windows service.

Relevant package, tooling and runtime versions
Packages

  • Microsoft.Extensions.Hosting 7.0.0
  • Microsoft.Extensions.Hosting.WindowsServices 7.0.0
  • Serilog 2.12.0
  • Serilog.AspNetCore 6.1.0
  • Serilog.Settings.Configuration 3.4.0
  • Serilog.SInks.Console 4.1.0

Platform

  • Windows Server 2016

Additional context
The .net 7 asp web api is deployed as a single self contained application.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions