question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable wrapping of an existing NLog.Logger as a Microsoft.Extensions.Logging.ILogger

See original GitHub issue

Type (choose one):

  • Feature request

NLog version: 4.7.5 NLog.Extensions.Logging version: 1.6.5 NLog.Web.AspNetCore version: not used

Platform: .NET Core 3.1

Based on https://github.com/NLog/NLog/wiki/EventProperties-Layout-Renderer#logger-withproperty-or-setproperty I am creating a logger with event properties specified during logger creation:

// Creates an NLog.Logger
NLog.Logger propertyLogger = LogManager.GetCurrentClassLogger(typeof(MyLogger)).WithProperty("key1", "val1");

In my code base, I am already using the Microsoft.Extensions.Logging.ILogger<T> abstraction, and I would like to use this NLog powered propertyLogger as an instance implementing this MEL.ILogger interface (to keep things clean). Example:

// I am looking for something like this
Microsoft.Extensions.Logging.ILogger melLogger = NLogLogger.Wrap(propertyLogger);

I have been looking around in the NLog.Extensions.Logging project to see if I could find a way to wrap an existing instance of an NLog.Logger as an MEL.ILogger, but I have not had any luck. The closest I get is the NLogLogger type which is marked as internal and therefore cannot be instantiated “outside”.

Questions:

A) Is is possible to wrap an existing instance of an NLog.Logger as an MEL.ILogger today? If so, how should that be done?

B) If not, would it be an option for you to provide framework support for this? I am not sure if it is as simple as updating the access modifier on NLogLogger from internal to public? Probably not 😃

Thanks in advance for your help.

Best regards, Lasse ( a 10+ years happy user of NLog 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

0reactions
swlassecommented, Nov 18, 2020

@snakefoot Alright, that sounds good. Yeah, naming is hard! If I get the time, I will see what I can come up with. I am glad for the input you have provided on this issue, and I believe I have the information I need to move on. Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Convert NLog.ILogger to Microsoft.Extensions.Logging
I know it can be quite easily done using NLog. Extensions. Logging package, adding NLog as provider: ILoggerFactory factory = LoggerFactory.
Read more >
Logging in C# - .NET
To create logs, use an ILogger<TCategoryName> object from DI. The following example: Creates a logger, ILogger<Worker> , which uses a log ...
Read more >
Logging in .NET Core and ASP.NET Core
Learn how to use the logging framework provided by the Microsoft.Extensions.Logging NuGet package.
Read more >
Essential .NET - Logging with .NET Core
LogX is called. A general approach to solving this is to possibly wrap Logger so as to catch the exception. However, you might...
Read more >
High-performance logging - .NET
Learn how to use LoggerMessage to create cacheable delegates that require fewer object allocations for high-performance logging scenarios.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found