1

Referencing Is it possible to display Serilog log in the program's GUI? I have this solution working well for me, however I cannot make it work using an appsettings.json configuration. I've variations along the lines of

  {
    "Name": "Sink",
    "Args": {
      "logEventSink": "$sink"
  }
  

with and without the $. The class for this is in my application and the application is in the using statement. I would be grateful for guidance on this.

azpc
  • 500
  • 9
  • 11

1 Answers1

1

First of all, you should create a configuration extension method that creates and configures sink. Because Serilog configuration reader looks for extension methods that accept LoggerSinkConfiguration as a first argument:

public static class InMemorySinkConfigurationExtensions
{
    public static LoggerConfiguration InMemory(
              this LoggerSinkConfiguration loggerConfiguration)
    {
        return loggerConfiguration.Sink(new InMemorySink());
    }
}

Next step - add assembly that has this method declared to the Using section of Serilog configuration:

{
  "Serilog": {
    "Using": [ "NameOfYourAssembly" ],
    // etc
}

And finally, you can add your sink configuration:

{
  "Serilog": {
    "Using": [ "NameOfYourAssembly" ],
    "MinimumLevel": {
      "Default": "Debug"
    },
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "InMemory", // should match name of extension method
        "Args": {
           // put here arguments for confuguration extension method
        }
      }
    ]
  }
}

Also, keep in mind that mentioned implementation of InMemorySink will not work with the configuration file because you don't have reference to the instance of this sink. A dirty way to fix this would be making the Events queue static. Otherwise use sink arguments to specify communication settings.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thanks for the response. You finish with the statement that the InMemorySink will not work with the configuration file. If I understand that correctly then the rest of your answer does not apply in this specific instance. Indeed I tried this and many variations without success. You also state in this case to use sink arguments to specify communication settings. Are you saying there is a way to make this work? If so could you expand on how to use communication settings to make this work? Thanks again. – azpc Jan 23 '21 at 15:54
  • @azpc this is the exact code which you should use to configure custom sink with appsettings file. Another problem is `InMemorySink` implementation which you are using. That one is not good. Make events static if you want to use it – Sergey Berezovskiy Jan 23 '21 at 16:51
  • I thank you for the response, however I'm still missing if what I'm trying to do will work and how to make it work. I tried what I believe your guidance is and many variations of it and I'm still unable to make it work. The sink works very well in my windows forms application in a textbox as long as the configuration is read from within the application itself. However it never hooks up when reading the configuration from the appsettings.json. – azpc Jan 24 '21 at 17:10
  • @azpc sorry, I don't understand what is your problem. Just follow the answer - it works – Sergey Berezovskiy Jan 24 '21 at 17:29