8

P.S. The code below works fine. The problem was between the monitor and the chair. Answer by @jpgrassi sent me in the right direction to resolve it.

I have the following in program.cs:

public class Program {
    public static void Main(string[] args) {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })
            .UseStartup<Startup>();

}

And in appsettings.json:

{
  "Serilog": {

    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Default": "Information",
        "Microsoft": "Information",
        "System": "Information"
      }
    },

    "WriteTo": [
      {
        "Name": "RollingFile",
        "Args": {
          "pathFormat": "log-{Date}.txt",
        }
      }
    ]
  },

  "AllowedHosts": "*"
}

In the controller, I write out a log _logger.LogWarning("foo");, but the file is not written out anywhere, there aren't any error that I can see.

I've imported following packages from Nuget: Serilog, Serilog.AspNetCore, Serilog.Settings.Configuration, Serilog.Sinks.RollingFile.

What am I missing?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • @CamiloTerevinto I don't think so on either 1 or 2 because I look for file writes using Process Monitor and don't see anything at all. – AngryHacker Mar 04 '19 at 19:22
  • I just had a hunch it was because of missing `Using:` prop on the config, but just tried with the same settings as you have and still works. Perhaps the config is getting overwritten somehow in your app? Can you debug `config.ReadFrom.Configuration(ctx.Configuration); ` and check if the Providers passed contain the Serilog configs above? – jpgrassi Mar 04 '19 at 20:21
  • Completely irrelevant to your question I'm afraid, but your `Override` section is redundant here as it's all the same level as the default. Your config looks generally sensible though – rbennett485 Mar 04 '19 at 22:12
  • @AngryHacker did my answer help you with solving anything? I figure yes since you accept it.. but just curious. – jpgrassi Mar 05 '19 at 20:46
  • @jpgrassi The code actually worked to begin with, but it was writing by default to c:\program files\iis express and failing because of permissions. And it wasn't showing up in ProcMon for whatever reason (maybe my filter was too tight). I changed the log name to be more unique and then it did show up in ProcMon. So, your answer helped crystallize for me that the code was working fine. – AngryHacker Mar 05 '19 at 22:01
  • Ah, I see. Thanks for the feedback. Just wanted to know if my answer provided something that helped in the end. It's frustrating that one spends time trying to actually help and then get downvotes. I was pretty sure the problem was the missing `Using` that's why I posted the answer. Now I've discovered that it's irrelevant so.. at least learned something :) I'll edit it and provide some context.. if I get more downvotes I'll delete it, since doesn't make sense anyway to leave it there. – jpgrassi Mar 05 '19 at 22:15
  • @CamiloTerevinto Edited. – AngryHacker Mar 05 '19 at 23:40

1 Answers1

6

I posted the original answer assuming the problem was the lack of the Using property in the Serilog configuration. I always had it in my projects and it was the only thing missing in OP's settings. But, after posting the answer I tried different configuration alternatives, including the same as in the question and the logs were still being produced. Reading more, I found out that the Using property is not necessary. From the serilog-settings-configuration package:

(This package implements a convention using DependencyContext to find any package with Serilog anywhere in the name and pulls configuration methods from it, so the Using example above is redundant.) Source: https://github.com/serilog/serilog-settings-configuration

As it turns out (see question comments) the real issue was log file location and write permissions. Code-wise everything was already working.

I'll keep the original answer here, as a matter of history, but its content does not actually fix anything, as nothing was broken in the first place.


Original answer:

I believe the problem is because you did not specify in your appsettings to actually use the RollingFile sink. I just quickly created a new app and it works:

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.RollingFile" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "RollingFile",
        "Args": { "pathFormat": "log-{Date}.txt" }
      }
    ],
    "Properties": {
      "Application": "Sample"
    }
  }

Using the following NuGet packages:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
    <PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
  </ItemGroup>

This logs to a file in the root directory of the app log-20190304.txt.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
jpgrassi
  • 5,482
  • 2
  • 36
  • 55
  • Well, technically you are right. But it seems it helped OP as he accepted the answer. I answered it with my best intention and I was honest enough to come back after spending almost an hour trying different configuration possibilities and they all work. What do you suggest, to delete the answer or? Being just critical and not providing a suggestion also doesn’t help anyone, I believe. – jpgrassi Mar 05 '19 at 19:58
  • Agreed. I edited it and added more context. but I could also delete it since it doesn't benefit anyone. – jpgrassi Mar 05 '19 at 22:30
  • Comments cleaned up, here and question – Camilo Terevinto Mar 05 '19 at 23:47
  • My log file was read-only for some reason. No idea how that happened. Your mention of write permissions reminded me to check for that, so thanks for keeping this answer around. – Eric Eskildsen Dec 16 '20 at 16:58
  • I have the same configuration like you, but log file is empty. – Kate Jun 24 '21 at 16:58