0

I'm just starting with Serilog. Despite all the code samples/tut's, I've found online I just can't get it to output to file (the file isn't even created). My app is a Web API (.NET Core 3.1), I'm referencing

Serilog.AspNetCore(3.4.0)
Serilog.Settings.Configurations (3.1.0)
Serilog.Sinks.File (4.1.0)

My appsettings.json:

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Warning",
        "System": "Warning"
      },
      "WriteTo": [
        {
          "Name": "File",
          "Args": {
            "path": "C:\\DEV\\Logs\\mylog.txt",
            "rollingInterval": "Day"
          }
        }
      ]
    }
  },
  "AllowedHosts": "*"
}

My Program.cs

public static void Main(string[] args)
    {
        Serilog.Debugging.SelfLog.Enable(Console.Out);

        //Read Configuration from appSettings
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();
        //Initialize Logger
        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(config)
            .CreateLogger();
        
        try
        {
            CreateHostBuilder(args).Build().Run();
            Log.Information("Application started!");
        }
        catch (Exception e)
        {
            Log.Fatal(e, "Application failed to start.");
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

My controller

public void Post(SampleRequest request)
{
    Log.Information("Received request {@request}", request);
}

Not even the Selflog is writing anything to Visual Studio output console Serilog.Debugging.SelfLog.Enable(Console.Out);

2 Answers 2

1

Try and enable SelfLog which should help you pinpoint what is going wrong. This call is slightly different to yours.

Add this in Program.cs just after you call .CreateLogger();

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

More details - https://github.com/serilog/serilog/wiki/Debugging-and-Diagnostics

I have Serilog logging to a rolling file in a .Net Core 3.1 app.

These are my nuget references:

<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.1-dev-00771" />

Also, just noticed you don't seem to have a Using section in your appsettings.json:

"Serilog": {
    "Using": [ "Serilog.Sinks.RollingFile" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "C:\\Logs\\ScreenPop\\Log-{Date}.txt"
        }
      }
    ]
  },
Sign up to request clarification or add additional context in comments.

3 Comments

Thx for the fast response. I can't find where exactly, but I read that Using directive in appsettings.json isn't necessary. Nevertheless, I've included it ("Using": [ "Serilog.Sinks.File" ]) alongside with the SelfLog line of code you provided, but still nothing :( . Neither my log file is created nor anything is debugged out to the visual studio console. Am I looking in the right place for selflog debug messages (Output windowin VS)?
@cagi yes that is the correct place. This is one downside I have found with Serilog, when it's not working, you get nothing to assist.
another idea, does the application have write permissions to the folder?
0

I figured out what was the problem. I copied settings from some blog and the "WriteTo" element was actually nested inside the "MinimumLevel" one. The correct settings would be:

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Warning",
        "System": "Warning"
        }
      },
      "WriteTo": [
        {
          "Name": "File",
          "Args": {
            "path": "C:\\DEV\\Logs\\mylog-.txt",
            "rollingInterval": "Day"
          }
        }
      ]
  },
  "AllowedHosts": "*"
}

Comments

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.