5

I am trying to implement logging in a .Net Core API and do not see the log file anywhere.

I'm using Microsoft.Extensions.Logging; and theILogger factory method.

In my Program.cs I have...

   var host = new WebHostBuilder()
           .UseApplicationInsights()
           .UseKestrel()
           .UseContentRoot(Directory.GetCurrentDirectory())
           .UseIISIntegration()
           .UseStartup<Startup>()
           .Build();

        host.Run();

In my Startup.cs I have this in the Configure method...

        loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        loggerFactory.AddEventSourceLogger();

In my appsettings.json I have this...

  "Logging": {
   "IncludeScopes": false,
  "LogLevel": {
  "Default": "Debug",
  "System": "Information",
  "Microsoft": "Information"
   }
  }

In my controller, I inject the logger and called the method this way...

   _logger.LogError("ERROR_MESSAGE_STRING");

The LogError method is being called with the error string but I do not see the log. I would expect to see the log error file somewhere. In the bin directory maybe?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
John
  • 371
  • 2
  • 4
  • 16
  • It logs to the console. This is perfectly fine, you should redirect your process stdout to a file. – Jazzwave06 Aug 22 '19 at 17:01
  • 1
    you aren't writing to a file. look at your your `.Add*` calls – Daniel A. White Aug 22 '19 at 17:01
  • This will be on an Azure service. Will it redirect to a datablob? Does that require another Add? – John Aug 22 '19 at 17:04
  • 1
    Oh wait a minute, the AddApplicationInsights() will log to that service within Azure right? – John Aug 22 '19 at 17:15
  • 1
    Yes. You have literally declared that logs will go to four potential destinations: App Insights, the console, the debug output, and the event viewer. Now, not all of these destinations will be relevant in all cases. Debug will only apply when debugging. Console is only applicable if there's some stdout. Event viewer will only apply if you're running on a Windows box. As such, App Insights is the only guaranteed destination. Regardless, none of these are file-based, so not sure why you'd think there should be a file somewhere. – Chris Pratt Aug 22 '19 at 19:05
  • AI should be sufficient! Currently though I'm not seeing exceptions that I'm intentionally causing to test the log to AI. Just calling that LogError method should enough right? I see the exceptions in Live Metrics on Azure but not in the AI app. – John Aug 22 '19 at 19:51

2 Answers2

4

If you want to log to a file then you'll need to add a call to

loggerFactory.AddFile

If you haven't installed a provider that logs to file then you'll need to.. i don't have any specific recommendations ("recommend me a library that..." is off topic for SO) but people do ask about logging to file without using a third party lib - for example here: How to log to a file without using third party logger in .Net Core?

You'll also doubtless turn up a wealth of hits if you search for things like "net core log to file"

The places you've told your loggerFactory to include in manufactured loggers don't have a file based sink at the moment. Every time your factory manufactures a logger it produces one that targets these:

loggerFactory.AddApplicationInsights //Microsoft azure cloud application insights service            
loggerFactory.AddConsole //the standard output of the command line
loggerFactory.AddDebug //for example, visual studio debug output window (i think)
loggerFactory.AddEventSourceLogger //for example, the windows performance monitoring system

For more information on the logging destinations you have configured and where their info can be found see this page of the fine manual: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2#built-in-logging-providers

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • 1
    ILoggerFactory does not seem to have an AddFile() – John Aug 22 '19 at 17:05
  • @John You could implement one, the interface is [pretty easy](https://github.com/aspnet/Extensions/blob/master/src/Logging/Logging.Abstractions/src/ILogger.cs). Or see if someone else has implemented one. Or use a logging library such as Serilog or NLog, both of which have file logging. – mason Aug 22 '19 at 17:11
  • Sorry, just noticed this and added a bit to the answer to cover it – Caius Jard Aug 22 '19 at 17:14
-2

Just calling that LogError method should enough right?

Calling LogError with .UseApplicationInsights() will be enough.

I see the exceptions in Live Metrics on Azure but not in the AI app

For Live Metrics, it is real-time, and you could see the Sample Telemetry directly after you send request, for for logs in Application Insights Search, it will have some deply, you may consider checking the log after about 5 minutes.

Edward
  • 28,296
  • 11
  • 76
  • 121