sgryphon / essential-diagnostics

Essential.Diagnostics contains additional trace listeners, filters and utility classes for the .NET Framework System.Diagnostics trace logging. Included are colored console, SQL database, rolling file, Seq logging server, and in-memory trace listeners, expression filters, activity and logical operation scopes, and configuration file monitoring.
Microsoft Reciprocal License
67 stars 25 forks source link

ASP.NET ILogger adapter #35

Open dzmitry-lahoda opened 4 years ago

dzmitry-lahoda commented 4 years ago

Could we have extension method onto TraceSource which exactly same as ILogger, but with no dependency on ASP.NET. So kind of reference free drop in replacement.

sgryphon commented 3 years ago

I'm not really sure what this means.

Usually the Adapter pattern is used to adapt from one pattern to another. i.e. public class TraceSourceAdapterLogger : ILogger { public TraceSourceAdapterLogger(TraceSource traceSource)...

However, this already exists, TraceSourceLoggerProvider.

If you configure TraceSourceLoggerProvider for a modern app that is using Microsoft.Extensions.Logging ILogger, then it will write through to the trace source (actually a hierarchy of trace sources, based on the name), and then through to any attached listeners, including the listeners from Essential Diagnostics.

However there is no automatic configuration, i.e. it won't load XML property settings, etc. This means you need to wire everything up in code. You can do this at the host level and then inject, or could even use reflection to load dynamically, but would have to do all that yourself.

I would just use ILogger, and I have already ported some of the TraceListeners across to equivalent LoggerProviders in my Essential.Logging project.