简体   繁体   中英

Serilog not logging to file ASP.Net Core 3.2

I am trying to log to a file using Serilog.AspNetCore 3.2.0 in my WebAssembly Blazor applications.

I have used Nicholas Blumhardt's blog entry here;

Setting up Serilog in ASP.NET Core 3.2

In my Program.cs of the blazor.server application I have the following code;

    public static void Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
        .Enrich.FromLogContext()
        .WriteTo.Console(new RenderedCompactJsonFormatter())
        .WriteTo.File(new RenderedCompactJsonFormatter(), "/logs/log.ndjson")
        .CreateLogger();
        try
        {
            Log.Information("Starting up");
            BuildWebHost(args).Run();
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Application start-up failed");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }


    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build())
            .UseStartup<Startup>()
            .Build();

I have the following packages installed;

Serilog.AspNetCore 3.2.0 Serilog.Sinks.File 4.1.0 Serilog.Sinks.MSSqlServer 5.5.0

(I include the MSSql Sink reference for completness as my next step is to look to log to a db).

However, when I debug locally and also when I publish to a server in IIS no log.ndjson is being created?

By default the file will be created in the project directory if you run from visual studio. so it all depends how are you running the application. so you have to change the path for file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM