简体   繁体   中英

how to log custom messages to azure portal analytics monitoring logs

I am trying to add custom logs in my c# asp dot net core web api. I am able to find the api calls logs in Azure portal -> application insights -> logs.

But i am not able to find the custom logs i am entering using below code. whats the place to search for them.

public async Task Invoke(HttpContext httpContext)
{
    // First, get the incoming request
    var request = await FormatRequest(httpContext.Request);

    // TODO: Save log to chosen datastore
    _logger.LogInformation('custommessage101');

    // ------
}

In log analytics query editor i used below query but it didnt fetch anything. Is it even the right place(Azure portal -> application insights -> logs) i am looking at ?

requests | search "custommessage101"

It may be that when you configured your logger you set the log level higher than information.

The following set the log level so that Information logging will be stored:

builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Information);

Messages logged to the ILogger interface end up as traces in application insights. An example query would be:

traces | where message == "custommessage101"

Another option would be the Search :

在此处输入图片说明

The default log level for messages to application insights is set to Warning . As Shiraz point out, you need to set it to informational. You can do that using code as shown by Shiraz or by adjusting the appsettings.json file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM