Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

c# - Display Serilog log in the program's GUI using appsettings.json configuration

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.

question from:https://stackoverflow.com/questions/65854267/display-serilog-log-in-the-programs-gui-using-appsettings-json-configuration

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...