Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • mmaty 109 posts 281 karma points
    Feb 04, 2022 @ 11:18
    mmaty
    0

    Umbraco 9: Can't write to custom serilog sink

    Hi everyone,

    I want to write to a custom serilog sink, which logs errors into EMails.

    I wrote a custom serilog sink based on Serilog sample code and tested the sink with a .NET 5.0 console app. It works nicely. I then added the sink package and exactly the same configuration into the appsettings.json file of my umbraco project. The sink is simply ignored.

    • I use Umbraco 9.1.2.

    • Setting a breakpoint shows, that the Extension method is never called.

    • The debug log shows, that the assembly has been loaded.

    • Entering a wrong assembly name into the Using line leads to an exception, which proves, that the Serilog configuration is read.

    • I followed the instructions given here: https://our.umbraco.com/documentation/reference/V9-Config/Serilog/

    This is my config:

        "Serilog": {
        "Using": [ "Formfakten.Serilog.EmailPickup" ],
        "MinimumLevel": {
            "Default": "Debug",
            "Override": {
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information",
                "System": "Warning"
            }
        },
        "WriteTo": [
            {
                "Name": "LaykitEmailPickup",
                "Args": {
                    "fromEmail": "[email protected]",
                    "toEmail": "[email protected]",
                    "pickupDirectory": "/App_Data/MailCache",
                    "subject": "Laykit-Fehler",
                    "restrictedToMinimumLevel": "Error"
                }
            }
        ]
    }
    

    EDIT: In the Umbraco Sources in SerilogLogger.CreateWithDefaultConfiguration we see the following lines:

    var loggerConfig = new LoggerConfiguration()
        .MinimalConfiguration(hostingEnvironment, loggingConfiguration, configuration)
        .ReadFrom.Configuration(configuration);
    

    This ends in a Serilog function

    Serilog.Settings.Configuration.dll ! Serilog.Settings.Configuration.ConfigurationReader.SelectConfigurationMethod()
    

    I can see there, that the name of the ConfigurationMethod is not "LaykitEmailPickup", but "Async". The parameters are the right ones. Changing the name in the debugger from "Async" to the correct name "LaykitEmailPickup" makes the Sink working. Looks like a bug.

    WTF? I set a breakpoint in Startup.cs, and watched at _config. This variable should represent the configuration as entered in the appsettings.json file. If I put the following expression in the Watch window

    _config.GetSection("Serilog:WriteTo:0:Name")
    

    the result is "Key"="Name" and "Value"="Async". The value should definitely be "LaykitEmailPickup" (see my appsettings.json above). Changing the value at this point results in a working condition.

    _config["Serilog:WriteTo:0:Name"] = "LaykitEmailPickup";
    

    But that's not the way ist should be... Any Ideas?

    Mirko

  • mmaty 109 posts 281 karma points
    Feb 04, 2022 @ 14:34
    mmaty
    0
  • mmaty 109 posts 281 karma points
    Feb 04, 2022 @ 15:31
    mmaty
    0

    Whoever stumbles upon this problem, and other problems, like "My smtp configuration always returns 'localhost', even if I declared the host to be 'xx.yy.com'": Umbraco constructs a development version of appsettings.json with a lot of unnecessary defaults. This is the reason, why it returns "Async" as sink name.

    The debug version overrides the standard version.

Please Sign in or register to post replies

Write your reply to:

Draft