我正在嘗試使用 Nlog.Config 將帶有日志檔案的日志檔案夾添加到我的專案的 wwwroot 檔案夾中。代碼是 XML 格式的,我對此一無所知。
<?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">
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target name="allfile" xsi:type="File"
fileName="\DemoLogs\nlog-all-${shortdate}.log"/> <!-- fileName needs a change -->
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
</rules>
uj5u.com熱心網友回復:
問題解決了,我使用了用戶發送的鏈接:Rolf Kristensen,我發現${aspnet-webrootpath}\yourPath可以完成這項作業。我更改了代碼,請參閱下面的更改。
變化:
- 更改
fileName="\DemoLogs\nlog-all-${shortdate}.log"為fileName="${aspnet-webrootpath}\DemoLogs\nlog-all-${shortdate}.log"創建新鏈接。 - 添加
throwConfigExceptions="true"到<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwConfigExceptions="true">除錯中。
錯誤:
當我運行程式時,它給了我 3 個內部錯誤:
- NLogConfigurationException:'FileTarget' 無法分配屬性'FileName'='${aspnet-webrootpath}\DemoLogs\nlog-all-${shortdate}.log'。錯誤:無法決議包含型別的布局:aspnet-webrootpath
- NLogConfigurationException:無法決議包含型別的布局:aspnet-webrootpath
- ArgumentException:LayoutRenderer 型別別名未知:“aspnet-webrootpath”。不包括擴展 NLog.Web.AspNetCore?
解決方案
為了修復這些錯誤,我使用了 HitHub 的解決方案并將其添加到代碼中:
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
最終代碼:
下面的代碼 100% 作業,試了一下,在wwwroot檔案夾中保存了一個日志檔案。
<?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"
throwConfigExceptions="true">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target name="allfile" xsi:type="File"
fileName="${aspnet-webrootpath}\DemoLogs\nlog-all-${shortdate}.log"/>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
</rules>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/511744.html
標籤:xmlasp.net 核心日志
