blog icon indicating copy to clipboard operation
blog copied to clipboard

How to add custom logging in ASP.NET Core

Open JuergenGutsch opened this issue 7 years ago • 11 comments
trafficstars

Written on 05.05.2017 08:16:06

URL: http://asp.net-hacker.rocks/2017/05/05/add-custom-logging-in-aspnetcore.html

JuergenGutsch avatar Nov 16 '18 21:11 JuergenGutsch

Comment written by Judah Gabriel Himango on 18.08.2017 22:22:29

Jurgen, is there a reason your LoggerProvider is trying to re-use instances (store them in ConcurrentDictionary and then use them again later)?

With your current code, I think you have a race condition in your ColoredConsoleLogger class because a single instance can used from 2 threads. For example, 2 concurrent requests on the same MVC controller will return the same ColoredConsoleLogger instance, because your LoggerProvider is trying to reuse instances.

It may be better to just create a new instance of ColoredConsoleLogger every time the LoggerProvider asks.

JuergenGutsch avatar Nov 16 '18 21:11 JuergenGutsch

Comment written by AndrzejM on 25.04.2018 17:49:43

In case every thread has each own logger we would not have race conditions... theoretically.

But in practice the external resource - console is an issue here. One of threads can change color while the second would use the color incorrectly to its log entry.

So IMHO the best choice is to stay with reusing logger between threads but protect last 'if' in Log method with lock.

JuergenGutsch avatar Nov 16 '18 21:11 JuergenGutsch

Comment written by Chirag Bhagat on 07.06.2018 17:16:04

How do I pass and read custom object in the log method implementation of my custom logger?

// Write log message
myCustomLogger.LogError("My Message", customDataObject);

// Log method implementation
public void Log(...)

In other words, How do I read customDataObject in above Log method?

Thanks in advance.

JuergenGutsch avatar Nov 16 '18 21:11 JuergenGutsch

The only bit that would help would be to know how to have something injected for you in the Logger or LoggerProvider...

SergeySagan avatar Aug 20 '19 05:08 SergeySagan

Hi @Serjster What exactly do you want to inject? Im not sure, if I get your question.

JuergenGutsch avatar Sep 09 '19 11:09 JuergenGutsch

Well, let's say that I needed to have a custom repository passed into ColoredConsoleLogger, I guess you can just add it as a param to the constructor of the logger and the factory will inject it for you without doing any more work?

SergeySagan avatar Sep 09 '19 14:09 SergeySagan

what do you mean with a repository in this context? A log sink? A log output target? If yes, this is what every logger needs to define ether hard coded or by using a config. NLog or Log4net do it with a config

JuergenGutsch avatar Sep 10 '19 09:09 JuergenGutsch

Nope I have my own custom logger and I need to pass in the repository for it into the logger. No worries. I figured it out by having it injected in the ConfigureServices in Startup then passing it down to the logger. BTW, questioning "why" someone is doing something never helps...

SergeySagan avatar Sep 10 '19 15:09 SergeySagan

@Serjster, for your case, I think you can pass the dependency to the constructor of the LoggerProvider and then pass down to the Logger since the Logger is created directly by the LoggerProvider.

Another way I can think of is to inject the service provider into the logger provider, and then create the logger using the service provider.

nguyenquyhy avatar Sep 15 '19 18:09 nguyenquyhy

Hello,

Just wonder if how can you use the custom logger in the controller?

Ethan0007 avatar Aug 11 '20 08:08 Ethan0007

Hi @Ethan0007 It should be automatically be used, as soon as it is configured.

JuergenGutsch avatar Aug 19 '20 14:08 JuergenGutsch