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

Add customized JSON Formmter in appsettings.json #28

Closed
XuefanWu opened this issue Nov 2, 2017 · 5 comments
Closed

Add customized JSON Formmter in appsettings.json #28

XuefanWu opened this issue Nov 2, 2017 · 5 comments

Comments

@XuefanWu
Copy link

XuefanWu commented Nov 2, 2017

I created a customized JSON Formatter which extends ITextFormatter
And try to add it to appsetting.json like this:
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "CustomizedJSONFormatter"
// "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
},

The default jsonFormatter works fine as shown in the commented line.
But I will get an error if I want to add the customized one:
InvalidCastException: Invalid cast from 'System.String' to 'Serilog.Formatting.ITextFormatter'.

Is there anyway to solve this?

Thanks!

@tsimbalar
Copy link
Member

You need to put in a full type name (the equivalent of typeof(CustomizedJSONFormatter).FullName ).

It must contain the type name and the full namespace before it.

For example : typeof(Serilog.Log).FullName -> "Serilog.Log".

Here is an example in the test suite :

https://github.com/serilog/serilog/blob/3cd376c8c75ed49ab307c50cd80beaa06cbaecb7/test/Serilog.Tests/Settings/SettingValueConversionsTests.cs#L48-L51

@XuefanWu
Copy link
Author

XuefanWu commented Nov 2, 2017

Yes. I have tried in that way too, I add the full namespace and everything. But it cause the same error.

@tsimbalar
Copy link
Member

Ooops, my bad, I gave you a link to the core Serilog repo ...

The behavior seems to be slightly different in in Serilog.Settings.Configuration, the one used for appsettings.json as I can see from this test.

From this test, I see you need to provide the full *Assembly qualified name, which should be something like this :

> typeof(Serilog.Log).AssemblyQualifiedName
"Serilog.Log, Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10"

From MSDN :

The assembly-qualified name of a type consists of the type name, including its namespace, followed by a comma, followed by the display name of the assembly. The display name of an assembly is obtained using the Assembly.FullName property.

I'm not sure Version, Culture and PublicKeyToken are really required though.

@XuefanWu
Copy link
Author

XuefanWu commented Nov 2, 2017

With the full Assembly qualified name, it works great now.
Thanks!!

@tsimbalar
Copy link
Member

I'll close the issue :)

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

2 participants