NLog 不写入 Azure Blob 存储
NLog not writing to Azure Blob Storage
我是 Azure 的新手,我正在尝试让我在 Azure 上的第一个 MVC Core 3.1 应用程序使用 NLog 写入 Azure Blob 存储。我相信我已经正确设置了它,但我在我的 Blob 存储中没有看到任何东西。
我正在使用以下文章来提供帮助。
https://www.taithienbo.com/securely-log-to-blob-storage-using-nlog-with-connection-string-in-key-vault
https://ozaksut.com/custom-logging-with-nlog
当我查看我的 Blob 存储时,我没有看到任何文件。我还假设我的 Blob 存储设置正确。
这是我的 proj 文件的一个片段,显示我有什么应该是正确的 NLog 包。
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Extensions.AzureBlobStorage" Version="3.0.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.2" />
<PackageReference Include="Oracle.EntityFrameworkCore" Version="3.19.80" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />
</ItemGroup>
<ItemGroup>
<Content Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
下面是我的 NLog.config 文件。如果我删除 throwExceptions="true"
然后网站会出现但不会创建日志文件。如果我保留预期,那么我的网站会出错,并且我会在 Azure 日志中看到一个错误,上面写着 Unhandled exception. System.FormatException: Settings must be of the form "name=value".
和
at NLog.Targets.BlobStorageTarget.CloudBlobService.Connect(String connectionString, String serviceUri, String tenantIdentity, String resourceIdentity, IDictionary`2 blobMetadata, IDictionary`2 blobTags)
at NLog.Targets.BlobStorageTarget.InitializeTarget()
at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
at NLog.Targets.Target.NLog.Internal.ISupportsInitialize.Initialize(LoggingConfiguration configuration)
at NLog.Config.LoggingConfiguration.InitializeAll()
at NLog.LogFactory.ReconfigExistingLoggers()
at NLog.LogFactory.set_Configuration(LoggingConfiguration value)
at NLog.LogFactory.LoadConfiguration(String configFile)
at NLog.LogManager.LoadConfiguration(String configFile)
at NLog.Web.NLogBuilder.ConfigureNLog(String configFileName)
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"
throwExceptions="true">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Extensions.AzureBlobStorage" />
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<variable name="LogDirectory" value="D:\logs\MvcProjectName"/>
<!-- the targets to write to -->
<targets>
<target xsi:type="File"
name="DefaultTarget"
fileName="${LogDirectory}\LogFile.log"
layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${message}"
archiveAboveSize="3145728"
archiveEvery="Day"
archiveFileName = "${LogDirectory}/Archive/{#}.log"
archiveNumbering = "DateAndSequence"
archiveDateFormat = "yyyyMMdd"
maxArchiveFiles = "50"
/>
<target xsi:type="AzureBlobStorage"
name="AzureBlob"
blobName="Log-${shortdate}.log"
container="epays-log"
connectionString="https://myservername.blob.core.windows.net/epays-log"
layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${message}">
</target>
<target name="ConsoleTarget"
xsi:type="Console"
layout="${longdate} ${logger:shortName=True} ${message} ${onexception:EXCEPTION OCCURRED\:${exception:format=type,message,StackTrace,method:maxInnerExceptionLevel=8:innerFormat=type,message,StackTrace,method}}"
/>
</targets>
<rules>
<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<logger name="*" minlevel="Debug" writeTo="DefaultTarget" />
<logger name="*" minlevel="Debug" writeTo="AzureBlob" />
<logger name="*" minlevel="Debug" writeTo="ConsoleTarget" />
</rules>
</nlog>
当我 运行 在本地站点时,我可以使用“DefaultTarget”设置让它登录到本地文件结构。
你的配置文件似乎是正确的,但你没有找到你的连接字符串在哪里。
转到您的存储帐户页面,在设置下找到 访问密钥,将连接字符串复制到您的 nlog.config
文件。
这是我这边的示例:
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"
internalLogLevel="info"
internalLogFile="C:\temp\fallback-log.txt"
throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.Extensions.AzureBlobStorage" />
</extensions>
<targets async="true">
<target
xsi:type="AzureBlobStorage"
name="azure"
layout="${longdate:universalTime=true} ${level:uppercase=true} - ${logger}: ${message} ${exception:format=tostring}"
connectionString="your-connection-string-goes-here"
container="logs"
blobName="${date:universalTime=true:format=yy-MM-dd}.log" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="azure" />
</rules>
</nlog>
项目生成的日志文件:
我是 Azure 的新手,我正在尝试让我在 Azure 上的第一个 MVC Core 3.1 应用程序使用 NLog 写入 Azure Blob 存储。我相信我已经正确设置了它,但我在我的 Blob 存储中没有看到任何东西。
我正在使用以下文章来提供帮助。
https://www.taithienbo.com/securely-log-to-blob-storage-using-nlog-with-connection-string-in-key-vault https://ozaksut.com/custom-logging-with-nlog
当我查看我的 Blob 存储时,我没有看到任何文件。我还假设我的 Blob 存储设置正确。
这是我的 proj 文件的一个片段,显示我有什么应该是正确的 NLog 包。
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Extensions.AzureBlobStorage" Version="3.0.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.2" />
<PackageReference Include="Oracle.EntityFrameworkCore" Version="3.19.80" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="System.ServiceModel.Duplex" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Http" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.*" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.*" />
</ItemGroup>
<ItemGroup>
<Content Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
下面是我的 NLog.config 文件。如果我删除 throwExceptions="true"
然后网站会出现但不会创建日志文件。如果我保留预期,那么我的网站会出错,并且我会在 Azure 日志中看到一个错误,上面写着 Unhandled exception. System.FormatException: Settings must be of the form "name=value".
和
at NLog.Targets.BlobStorageTarget.CloudBlobService.Connect(String connectionString, String serviceUri, String tenantIdentity, String resourceIdentity, IDictionary`2 blobMetadata, IDictionary`2 blobTags)
at NLog.Targets.BlobStorageTarget.InitializeTarget()
at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
at NLog.Targets.Target.NLog.Internal.ISupportsInitialize.Initialize(LoggingConfiguration configuration)
at NLog.Config.LoggingConfiguration.InitializeAll()
at NLog.LogFactory.ReconfigExistingLoggers()
at NLog.LogFactory.set_Configuration(LoggingConfiguration value)
at NLog.LogFactory.LoadConfiguration(String configFile)
at NLog.LogManager.LoadConfiguration(String configFile)
at NLog.Web.NLogBuilder.ConfigureNLog(String configFileName)
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"
throwExceptions="true">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Extensions.AzureBlobStorage" />
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<variable name="LogDirectory" value="D:\logs\MvcProjectName"/>
<!-- the targets to write to -->
<targets>
<target xsi:type="File"
name="DefaultTarget"
fileName="${LogDirectory}\LogFile.log"
layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${message}"
archiveAboveSize="3145728"
archiveEvery="Day"
archiveFileName = "${LogDirectory}/Archive/{#}.log"
archiveNumbering = "DateAndSequence"
archiveDateFormat = "yyyyMMdd"
maxArchiveFiles = "50"
/>
<target xsi:type="AzureBlobStorage"
name="AzureBlob"
blobName="Log-${shortdate}.log"
container="epays-log"
connectionString="https://myservername.blob.core.windows.net/epays-log"
layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${message}">
</target>
<target name="ConsoleTarget"
xsi:type="Console"
layout="${longdate} ${logger:shortName=True} ${message} ${onexception:EXCEPTION OCCURRED\:${exception:format=type,message,StackTrace,method:maxInnerExceptionLevel=8:innerFormat=type,message,StackTrace,method}}"
/>
</targets>
<rules>
<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<logger name="*" minlevel="Debug" writeTo="DefaultTarget" />
<logger name="*" minlevel="Debug" writeTo="AzureBlob" />
<logger name="*" minlevel="Debug" writeTo="ConsoleTarget" />
</rules>
</nlog>
当我 运行 在本地站点时,我可以使用“DefaultTarget”设置让它登录到本地文件结构。
你的配置文件似乎是正确的,但你没有找到你的连接字符串在哪里。
转到您的存储帐户页面,在设置下找到 访问密钥,将连接字符串复制到您的 nlog.config
文件。
这是我这边的示例:
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" internalLogLevel="info" internalLogFile="C:\temp\fallback-log.txt" throwConfigExceptions="true"> <extensions> <add assembly="NLog.Web.AspNetCore"/> <add assembly="NLog.Extensions.AzureBlobStorage" /> </extensions> <targets async="true"> <target xsi:type="AzureBlobStorage" name="azure" layout="${longdate:universalTime=true} ${level:uppercase=true} - ${logger}: ${message} ${exception:format=tostring}" connectionString="your-connection-string-goes-here" container="logs" blobName="${date:universalTime=true:format=yy-MM-dd}.log" /> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="azure" /> </rules> </nlog>
项目生成的日志文件: