Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I somehow pass formatter args in appsettings.json? #138

Closed
xneg opened this issue Mar 30, 2020 · 4 comments
Closed

Can I somehow pass formatter args in appsettings.json? #138

xneg opened this issue Mar 30, 2020 · 4 comments

Comments

@xneg
Copy link

xneg commented Mar 30, 2020

In code I can setup formatter like this

Log.Logger = new LoggerConfiguration()
                .WriteTo.Async(config =>
                {
                        var formatter = new ExceptionAsObjectJsonFormatter(renderMessage: true, inlineFields: true);
                        config.Console(formatter);
                })
                .CreateLogger();

Can I do the same thing in appsettings.json?
This one does not work:

"Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "D:/logs/{env}.deliverycashier/.log",
          "formatter": "Serilog.Formatting.Elasticsearch.ExceptionAsObjectJsonFormatter, Serilog.Formatting.Elasticsearch",
          "renderMessage": true,
          "inlineFields": true
        } 
      } 
] }
@cocowalla
Copy link
Contributor

Hi!

There are a couple of similar issues over in the Serilog.Settings.Configuration repo.

In a nutshell, it's not possible to pass parameters to constructors at present.

I haven't tried this, but I guess a kind of workaround would be to create your own class that extended the formatter in question and provided the values you want (with a parameterless ctor!)

@xneg
Copy link
Author

xneg commented Mar 30, 2020

Ok, thank you.
I was thinking about possibility you proposed. But I thought there is some ready solution.
Sorry for opening duplicate issue.

@cocowalla
Copy link
Contributor

Not a problem, I'm happy to help 😄

@heysaad
Copy link

heysaad commented Jan 15, 2024

I think this is possible now. refer serilog/serilog-settings-configuration#225 (comment)

{
  "Name": "File",
  "Args": {
    "path": "log.json",
    "rollingInterval": "Day",
    "retainedFileCountLimit": 7,
    "formatter": {
      "type": "Serilog.Formatting.Json.JsonFormatter, Serilog",
      "renderMessage": true
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants