0

We are having a .NET 4.7.2 WebForms project that needs to consume a library. That library is also consumed by another application which is based on the Core platform i.e. .NET 6.0

We want to pass an ILogger instance to the library class from both Framework side and Core side. In the Framework side, we are able to create ILogger instance using the Microsoft.Extensions.Logging package and below code.

    var serviceCollection = new ServiceCollection();
    serviceCollection.AddLogging();
    var serviceProvider = serviceCollection.BuildServiceProvider();
    ILogger logger = serviceProvider.GetService<ILogger>();

But how do we pass the App Insights and Event Viewer providers to this logger from .NET Framework WebForms?

Jatin
  • 83
  • 7
  • 1
    Is there anything preventing you from doing the same thing in Web Forms? Or if you don't want to bother with a service collection just to create a logger, why not [follow the documentation](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#non-host-console-app) on how to create one using the factory? – mason Jan 11 '23 at 14:39
  • Thanks @mason This was very helpful. I missed out looking at the link for Non generic host application. Could you please let me know if the below code is proper or I can make it better? – Jatin Jan 11 '23 at 16:49
  • `ILogger logger1; using (var loggerFactory = LoggerFactory.Create(builder => { builder.AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; }).AddApplicationInsights(telemetry => telemetry.ConnectionString = "my-key", options => options = new ApplicationInsightsLoggerOptions());})) { logger1 = loggerFactory.CreateLogger("Pages"); logger1.LogInformation("abc"); } ` – Jatin Jan 11 '23 at 16:59
  • I really wanted to ask on how to set the TelemetryConfiguration and Options on the AddApplicationInsights extension method – Jatin Jan 11 '23 at 17:00
  • You tell me if it's correct or not - does it do what you want it to do? Is it free of unnecessary code? Is it clean? – mason Jan 11 '23 at 17:39
  • It does the job for me. I also feel its clean. Can I improve it? I do have a few questions though. – Jatin Jan 11 '23 at 18:03
  • why this returns a null logger? `serviceCollection.AddLogging(builder => builder .AddFilter("Default", LogLevel.Information) .AddFilter("Microsoft", LogLevel.Information) .AddFilter("System", LogLevel.Information) .AddEventLog(config => { config.LogName = "Pages"; config.SourceName = "Pages"; }) .AddApplicationInsights(telemetry => telemetry.ConnectionString = "my-key", options => options = new ApplicationInsightsLoggerOptions()));` – Jatin Jan 11 '23 at 18:05
  • And while using your approach, I have to log inside 'using' block only; else it gets flushed out. So when I will pass the logger to the library, I would have to write the library call inside this using. Could there be a better approach? – Jatin Jan 11 '23 at 18:07
  • I have no idea what you're talking about. If you're having a new problem, then ask a new question and adequately provide context, including a [mcve]. As for why you're getting a null logger - you haven't shown anything that would return a logger. You just registered a logger with the service collection. – mason Jan 11 '23 at 18:33
  • asked another question [here](https://stackoverflow.com/questions/75091781/get-an-ilogger-instance-from-servicecollection-in-a-non-host-net-framework-appl) – Jatin Jan 12 '23 at 04:59

0 Answers0