[英]Nlog with a class Library
我在一個單獨的地方有一個類庫和配置文件我想使用它所以我執行以下操作
LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(configurationFile , true);
LogManager.ReconfigExistingLoggers();
NLog 的配置是遠程 app.config 的一部分,看起來像
<configuration>
<!-- ... -->
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<!--
Log in a separate thread, possibly queueing up to
5000 messages. When the queue overflows, discard any
extra messages
-->
<target name="file" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard"
layout="${machinename} - ${level} - ${longdate} - ${callsite} - ${threadid} - ${message} ${exception:format=tostring}">
<target xsi:type="File" fileName="C:\logs/${level}.txt" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
</configuration>
配置對象LogManager.Configuration
總是有默認值,不知道如何解決這個問題。
創建第一個 NLog Logger 對象時,NLog 將嘗試自動加載 NLog.config(或 app.config)。 搜索多個位置:
https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-locations
您應該嘗試激活 NLog 內部記錄器,看看為什么它在不進行手動加載的情況下找不到“遠程 app.config”(可以通過代碼啟用):
https://github.com/NLog/NLog/wiki/Internal-Logging
不建議類庫本身嘗試修改全局 NLog 配置,因為它可能會使主應用程序感到驚訝。 如果想要為您的類庫設置一個獨立的 NLog 配置,那么您應該考慮創建一個獨立的 NLog LogFactory:
https://github.com/NLog/NLog/wiki/Configure-component-logging
如果要從非標准路徑(不是 app.config)加載 NLog.config。 然后你也可以這樣做:
https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.