Sending logs from .net core application running in App Service to log analytics

帅比萌擦擦* 提交于 2021-01-29 19:28:01

问题


I tried looking for the simplest way of my .net core 2.0 application to send app logs to log analytics workspace.

This application is running under App Service in azure, and I tried enabling the "Diagnostic Settings" and archiving the logs to log analytics.

However, I am not seeing my app custom logs messages in: AppServiceHTTPLogs .

I have used the following guide: https://azure.github.io/AppService/2019/11/01/App-Service-Integration-with-Azure-Monitor.html

My application is using ILoggerFactory:

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

Any idea of how to ship logs to there?


回答1:


I test and it works well in my site. You could refer to the following code: In startup.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    other codes...

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddAzureWebAppDiagnostics();
    loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information);
    loggerFactory.AddDebug();
    app.UseStaticFiles();

    other codes...
} 

In HomeController:

private readonly ILogger _logger;

public HomeController(ILoggerFactory loggerFactory)
{
     _logger = loggerFactory.CreateLogger<HomeController>();
}

public IActionResult Index()
{
     _logger.LogInformation("this is a information from ILogger...");
     return View();
}

Configure azure webapp Diagnostics settings as the article and then you will get the appservicehttplogs. Wait a few minutes, and it will be added to storage automatically.



来源:https://stackoverflow.com/questions/60397753/sending-logs-from-net-core-application-running-in-app-service-to-log-analytics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!