1

NET Developer I am currently working on a ASP NET MVC 5 library. The code will eventually be built into a DLL and shipped as a nuget package to be consumed else where.

My Problem: I am looking at how to have some method of logging in this file that is flexible and can be used by all the different users who consume it. After doing some research, it looks like most people who are working in some form of .NET have access to Microsoft.Extensions.Logging and ILogger. This seems to fit my requirements since it has logging for errors, informational, warnings, etc.

Is this how I properly use it? I currently have

var factory = new LoggerFactory();
ILogger logger = factory.CreateLogger("MyLogger");
// Now I can call the generic methods
log.LogInformation("This is an informational log");

Am I missing key functionality? Online I have seen individuals use code such as ILoggerFactory factory = new LoggerFactory().AddConsole(); but I do not have access to `AddConsole after instantiating a new LoggerFactory. I mostly just want a generic way to log events so that others can use my code and appropriate logs are created and I don't have to worry about custom implementation of logging, etc. I'm more focused on logging string messages.

mikeg
  • 59
  • 4
  • 1
    Usually you don't instantiate `LoggerFactory` you use Dependecy Injection and resolve `ILogger` in your classes. – Guru Stron Sep 01 '20 at 21:40
  • @mikeg You might find [How to get Microsoft.Extensions.Logging in console application using Serilog and AutoFac?](https://stackoverflow.com/q/41414796/11726740) useful, especially the accepted answer. – John Omielan Sep 01 '20 at 23:36

0 Answers0