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

How to add CategoryName to output #9

Closed
leastprivilege opened this issue Jul 17, 2015 · 19 comments
Closed

How to add CategoryName to output #9

leastprivilege opened this issue Jul 17, 2015 · 19 comments

Comments

@leastprivilege
Copy link

By default Serilog does not output the CategoryName e.g.:

var logger = loggerFactory.CreateLogger("test");

I tried a couple of templates - e.g.

.WriteTo.Trace(outputTemplate: "{Timestamp} [{Level}] ({CategoryName:l}) {Message}{NewLine}{Exception}")

but it does not work - any pointers?

thanks

@nblumhardt
Copy link
Member

Hi Dominick,

I'm not sure we have any handling for this currently - I'm not actually sure how CategoryName propagates through the logging pipeline to be honest.

Any insights here would be appreciated. Definitely sounds like something we should support, if it's native to m.f.Logging.

@leastprivilege
Copy link
Author

Well - I don't know either. Need to look at their source code. Thought that's pretty fundamental.

In LibLog I always used {Name} in the format string.

@leastprivilege
Copy link
Author

Could someone help out? @danroth27 @HaoK @rynowak

@rynowak
Copy link

rynowak commented Jul 29, 2015

@nblumhardt
Copy link
Member

Thanks @rynowak.

It looks like we should attach the CurrentScope's name as a property, if there is one, in here:

https://github.com/serilog/serilog-framework-logging/blob/dev/src/Serilog.Framework.Logging/SerilogLoggerProvider.cs#L41

@leastprivilege how is the scope name actually set, when calling an ILogger? I don't seem to have a BeginScope() method that allows it to be provided...

@nblumhardt
Copy link
Member

@leastprivilege sorry, I see that is in your original post. What I guess is throwing me off is how it fits with the hierarchical nature of the scopes, if it can only be set when creating the root logger? Cheers! :-)

@leastprivilege
Copy link
Author

I remember seeing something about nested scopes in the source code. But for now I would be happy if the simple "name" would work ;)

@leastprivilege
Copy link
Author

Was this ever resolved?

@nblumhardt
Copy link
Member

No, not as yet - any help here would be great.

@prasannavl
Copy link

Not sure if it was updated since this question was asked, but categoryName is sent in as context in Serilog.

Ref: https://github.com/serilog/serilog-framework-logging/blob/dev/src/Serilog.Framework.Logging/SerilogLogger.cs#L37

So it should be available from {SourceContext:l} ..

However, I'm wondering.. is there a way to conditionally emit it on the templates only if its available?
Doing something like

config.WriteTo.ColoredConsole(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] [{SourceContext:l}] {Message}{NewLine}{Exception}");

ends up with the verbatim string [{SourceContext:l}] when there is no SourceContext available.

@nblumhardt
Copy link
Member

@prasannavl I think you need to omit the :l for the behaviour you're looking for (output template tokens are literals by default).

@prasannavl
Copy link

@nblumhardt, if I omit it, it ends up with quotes around it.

@prasannavl
Copy link

@nblumhardt - Actually, omitting :l doesn't help. It still ends up with the verbatim string - which I think is the correct behavior. I'm just wondering if there is a way to remove it when the property is not available.

@nblumhardt
Copy link
Member

Interesting - are you using an old Serilog build? (Know that is unlikely - but the change to omit empty properties has been in there for a while, may be a bug here?)

@prasannavl
Copy link

Build info from project.lock.json :

      "Serilog.Framework.Logging/1.0.0-rc1-final-10071": {
        "type": "package",
        "dependencies": {
          "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
          "Serilog": "1.5.11"
        },

@stajs
Copy link

stajs commented Feb 4, 2016

However, I'm wondering.. is there a way to conditionally emit it on the templates only if its available?

I'm also interested in this.

@nblumhardt
Copy link
Member

Thanks for the notes and follow-ups.

This is a bug in ColoredConsoleSink: serilog/serilog#649

The other sinks (Console, Literate Console) shouldn't have the issue. Serilog.Sinks.LiterateConsole is by far my favourite console option now BTW, so worth trying out if you haven't already!

Hoping we'll get some traction on the issue linked above for 2.0 RTM (feel free to take a shot at it if you get an opportunity).

Thanks again!

@stajs
Copy link

stajs commented Feb 8, 2016

The other sinks (Console, Literate Console) shouldn't have the issue.

Tried it, much better! Thanks for the recommendation =)

@nblumhardt
Copy link
Member

Looks like this is covered by SourceContext. Thanks again all!

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

No branches or pull requests

5 participants