[英]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.