简体   繁体   English

Azure 应用服务诊断 blob 未记录基于 nlog 的日志

[英]Azure app service diagnostic blob not logging nlog based logs

在此处输入图像描述 I want to log nlog generated application logs in app service diagnostic blob [ie, Application Logging (Blob) ] but only default logs are printed not the nlog based custom logs but I can print Application Logging (Filesystem) when file target is added to nlog.config.我想在应用服务诊断 blob [即应用程序日志记录 (Blob)] 中记录 nlog 生成的应用程序日志,但只打印默认日志而不是基于 nlog 的自定义日志,但是当文件目标添加到 nlog 时我可以打印应用程序日志记录(文件系统) .config。 The problem is only with blob.问题仅在于blob。

nlog.config file: nlog.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwConfigExceptions="true"
      internalLogLevel="info"
      internalLogFile="d:\home\LogFiles\temp\internal-nlog-AspNetCore3.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <target xsi:type="Trace" name="String" layout="${level}\: ${logger}[0]${newline} |trace|     ${message}${exception:format=tostring}" />
    <target xsi:type="Console" name="lifetimeConsole" layout="${level}\: ${logger}[0]${newline} |console|     ${message}${exception:format=tostring}" />
  </targets>

 <rules>
      <logger name="*" minlevel="Trace" writeTo="lifetimeConsole,String" final="true"/>
    </rules>
</nlog>

program.cs file程序.cs文件

namespace testapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
            try
            {
                logger.Debug("init main");
                CreateHostBuilder(args).Build().Run();
            }
            catch (Exception exception)
            {
                logger.Error(exception, "Stopped program because of exception");
                throw;
            }
            finally
            {
                NLog.LogManager.Shutdown();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logging =>
            {
                logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                logging.AddConsole();
                logging.AddDebug();
                logging.AddAzureWebAppDiagnostics();
            })
              .UseNLog() // NLog: Setup NLog for Dependency injection
            .ConfigureServices(serviceCollection => serviceCollection
                    .Configure<AzureBlobLoggerOptions>(options =>
                    {
                        options.BlobName = "testlog.txt";
                    }))
              .ConfigureWebHostDefaults(webBuilder =>
              {
                  webBuilder.UseStartup<Startup>();
              }); 
    }

}

The Nlog based loggings are not logged in app service diagnostic blob, instead only default logging is printed.基于 Nlog 的日志不会记录在应用服务诊断 blob 中,而是仅打印默认日志。 Kindly help to resolve this issue.请帮助解决此问题。

Seems that System.Diagnostics.Trace-Target works best for ASP.NET-application and not for ASP.NetCore applications in Azure.似乎 System.Diagnostics.Trace-Target 最适合 ASP.NET 应用程序,而不是 Azure 中的 ASP.NetCore 应用程序。

When using AddAzureWebAppDiagnostics it will redirect all output written to Microsoft ILogger to FileSystem or Blob.使用AddAzureWebAppDiagnostics时,它将所有写入 Microsoft ILogger 的 output 重定向到 FileSystem 或 Blob。 But any output written to pure NLog Logger-objects will not be redirected.但是任何写入纯 NLog Logger 对象的 output 都不会被重定向。

Maybe the solution is to setup a NLog FileTarget writing to the HOME-directory:也许解决方案是设置一个 NLog FileTarget 写入 HOME 目录:

    <nlog>
         <targets async="true">
             <!-- Environment Variable %HOME% matches D:/Home -->
             <target type="file" name="appfile" filename="${environment:HOME:cached=true}/logfiles/application/app-${shortdate}-${processid}.txt" />
         </targets>
         <rules>
             <logger name="*" minLevel="Debug" writeTo="appFile" />
         </rules>
    </nlog>

See also: https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#stream-logs另请参阅: https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#stream-logs

For including Blob-output then take a look at NLog.Extensions.AzureBlobStorage要包含 Blob 输出,请查看NLog.Extensions.AzureBlobStorage

Alternative to Blob-output could be ApplicationInsights but it might have different pricing. Blob 输出的替代方案可能是ApplicationInsights ,但它可能有不同的定价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 应用服务日志和诊断设置之间的区别 - Difference between Azure App Service logs and Diagnostic settings 找不到Azure应用服务诊断日志应用程序日志记录Blob存储URL 404 - Azure App Service Diagnostics Logs Application Logging Blob Storage URL Not Found 404 Azure云服务(经典)-以任何方式将Diagnostic.Trace日志记录到BLOB存储中 - Azure Cloud Service (Classic) - Any way to log Diagnostic.Trace logs to BLOB storage 无法在Azure App Service上生成日志NLog内部日志文件 - Unable to generate log NLog internal logging file on Azure App Service Azure 应用服务应用服务日志 blob 保留不起作用 - Azure App Service app service logs blob retention not working 将 Azure 应用服务的 stdout 和 stderr 日志存储到 Azure Blob 存储中 - Store stdout and stderr logs of Azure App Service into Azure Blob Storage Power BI 和 Azure Web 应用诊断日志 - Power BI and Azure Web App diagnostic logs 带有Azure Appcenter的移动应用程序的诊断日志 - diagnostic logs for mobile app with azure appcenter 将Azure诊断日志写入blob存储的性能影响 - Performance impact of writing Azure diagnostic logs to blob storage 自动将App Service诊断日志记录到存储 - Automate App Service diagnostic logging to Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM