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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 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
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 sorry, I don't understand what is your problem. Just follow the answer - it works

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.