5

The default log entry of a .netcore app is being written to the stdout and interpreted by the GCP Stackdriver as an info message, even if it is an error. Is there a way to write .netcore errors to the stderr or configure them somehow so that Stackdriver will interpret them as errors?

Stackdriver log entry: Stackdriver logs

.netcore app log entry: netcore app logs

c69
  • 19,951
  • 7
  • 52
  • 82
Kiramm
  • 331
  • 3
  • 19
  • There's no default log entry, you have to configure it. What you posted is generated because somewhere, some code added Console logging. That may have been the generic host builder or something else. How did you configure logging? – Panagiotis Kanavos Jul 21 '20 at 14:48
  • 1
    You can use GCP's [ASP.NET Core integration](https://cloud.google.com/logging/docs/integrate/dotnet#use-aspnetcore) to send log events to StackDriver directly instead of going through stdout – Panagiotis Kanavos Jul 21 '20 at 14:51
  • @PanagiotisKanavos [looks like](https://weblog.west-wind.com/posts/2018/Dec/31/Dont-let-ASPNET-Core-Default-Console-Logging-Slow-your-App-down#where-does-default-logging-configuration-come-from) there is a default logging configuration starting from aspnetcore 2.2; I'll try to use GCP's ASP.NET Core integration although I don't understand how it will make Stackdriver interpret the severity correctly. – Kiramm Jul 21 '20 at 15:28
  • No there isn't. If, and *only* if, you use the generic or web host builder, do you get configuration, logging, dependency injection with default settings. You can add the exact same config through code, it's typically just a couple of lines. You can modify even those defaults though by calling eg `ConfigureLogging(builder=>.....)`. The link shows how to add StackDriver logging – Panagiotis Kanavos Jul 21 '20 at 15:30
  • 3
    If you insist on writing to console you'll have to *remove* the default console logger and add another one. The Microsoft.Extensions.Logging.Console logger is *very* basic, with a hard-coded format that writes parts of a single event in different lines. It can't be customised and isn't meant to - it's only meant to be the most basic default. Serilog's Console provider offers extensive customisation but why use the *console* when you can already send events to StackDriver? – Panagiotis Kanavos Jul 21 '20 at 15:33
  • 1
    @PanagiotisKanavos By the default log entry, I meant the default console logger, and I don't insist on logging to the console, so clearing the default provider and logging directly to Stackdriver if fine for me. I removed the default console logger and added Google's logger factory and the Stackdriver started interpreting log entries severity correctly, thanks – Kiramm Jul 22 '20 at 14:06
  • @Kiramm can you provide some sample code? I did like this but it did not work https://dotnetfiddle.net/byvuJX ``` var builder = WebApplication.CreateBuilder(args); builder.Services .AddGoogleDiagnosticsForAspNetCore(loggingOptions: new LoggingServiceOptions { Options = LoggingOptions.Create() }) .AddLogging(lb => { lb.ClearProviders(); lb.AddGoogle(); }); } ``` – Marsen Lin Jun 10 '22 at 09:42

1 Answers1

1

per Panagiotis Kanavos@:

If you insist on writing to console you'll have to remove the default console logger and add another one. The Microsoft.Extensions.Logging.Console logger is very basic, with a hard-coded format that writes parts of a single event in different lines. It can't be customised and isn't meant to - it's only meant to be the most basic default. Serilog's Console provider offers extensive customisation but why use the console when you can already send events to StackDriver?

This did solved the original issue, per Kiramm@:

I removed the default console logger and added Google's logger factory and the Stackdriver started interpreting log entries severity correctly

c69
  • 19,951
  • 7
  • 52
  • 82