Benjamin Podszun
Benjamin Podszun

Reputation: 9827

Unable to initialize Serilog.Sinks.EventLog in a SelfContained/PublishSingleFile/PublishTrimmed application

Goal: A single executable, trimmed (size matters for this use case), that at runtime is able to load the Console sink (works) and/or the EventLog sink (fails, my problem here).

Basic .csproj parts:

<PublishSingleFile>true</PublishSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishTrimmed>true</PublishTrimmed>

This still fails after trimming without a custom trimming configuration

Add to .csproj

<ItemGroup>
  <TrimmerRootDescriptor Include="TrimmerRoots.xml" />
</ItemGroup>

The TrimmerRoots.xml

<?xml version="1.0" encoding="utf-8" ?>
<linker>
  <assembly fullname="Serilog" />
  <assembly fullname="Serilog.Settings.Configuration" />
  <assembly fullname="Serilog.Sinks.Console" />
  <assembly fullname="Serilog.Sinks.EventLog" />
</linker>

At this point we're sooooo close. Unfortunately, while logging to the Console from the bundle works now, using say this appsettings.json

{
  "Serilog": {
    "Using": [ "Serilog", "Serilog.Sinks.Console", "Serilog.Settings.Configuration" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "Console" }
    ],
    "Enrich": [ "FromLogContext" ]
  }
}

logging to the EventLog silently fails using this appsettings.json

{
  "Serilog": {
    "Using": [ "Serilog", "Serilog.Sinks.EventLog", "Serilog.Settings.Configuration" ],
    "MinimumLevel": {
      "Default": "Debug"
    },
    "WriteTo": [
      {
        "Name": "EventLog",
        "Args": {
          "source": "Agent Updater",
          "logName": "Application",
          "manageEventSource": true
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
}

If I enable SeriLog self-logging using Serilog.Debugging.SelfLog.Enable(Console.Error); I get the following message:

2024-01-10T22:55:17.8478998Z Unable to find a method called EventLog for supplied arguments: logName, manageEventSource, source. Candidate methods are:
EventLog(, , , , , , , )
EventLog(, , , , , , )
  1. The argument names listed here correspond to the arguments in my appsettings.json (adding/removing stuff is reflected, i.e. if I add a machineName property to the appsettings.json I will see that here too)
  2. I don't understand why the EventLog(..) overloads here have no argument names
  3. These lines seem to reference the extension methods at https://github.com/serilog/serilog-sinks-eventlog/blob/master/src/Serilog.Sinks.EventLog/LoggerConfigurationEventLogExtensions.cs#L49 and https://github.com/serilog/serilog-sinks-eventlog/blob/master/src/Serilog.Sinks.EventLog/LoggerConfigurationEventLogExtensions.cs#L49 .. and all the arguments I didn't provide have defaults

Logging works perfectly fine in the debugger/before bundling and trimming this project. Any suggestions what is going wrong here at this point?

Upvotes: 1

Views: 113

Answers (0)

Related Questions