我有一個自定義LayoutRenderer
定義爲一個共同的組件,用於NLog
如下:NLOG定製LayoutRenderer不工作
[LayoutRenderer("appsettings")]
public class AppSettingsLayoutRenderer : LayoutRenderer {
[DefaultParameter]
public string Variable {
get;
set;
}
protected override void Append(StringBuilder builder, LogEventInfo logEvent) {
if (Variable == null) {
return;
}
var context = HttpContext.Current;
if (context == null) {
return;
}
builder.Append(Convert.ToString(ConfigurationManager.AppSettings[Variable], CultureInfo.InvariantCulture));
}
}
這種常見的組件在2個項目,其中我已經實現日誌引用。
第一個項目是一個ASP.NET/ASP.NET MVC網站,並配置爲頂級網站。
第二個項目是一個WCF項目,它託管IIS下的多個服務並配置爲另一個網站(IIS中的默認網站)下的虛擬目錄。
兩者都包含在它們的Web.config
以下:
<appSettings>
<add key="ErrorLogPath" value="D:\Logs" />
</appSettings>
兩個項目引用的共同組件(其中包含客戶LayoutRenderer
)。我已經驗證兩個項目的bin
文件夾實際上也包含程序集DLL。我通過在IIS中配置的路徑證實了這一點。
最後,這兩個項目包含以下NLog.config
文件旁邊的Web.config
文件:
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="Collective.Core" />
</extensions>
<targets>
<target name="file" xsi:type="File" layout="${longdate}|${level:uppercase=true}|${logger}|${message}${onexception:inner=${newline}${exception:format=tostring}}" fileName="${appsettings:ErrorLogPath}\${logger}\${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
相同的應用程序池下運行這兩個項目。
現在,我的問題:
對於第一個項目,該日誌出現在正確的位置,即D:\Logs\Logger.Name\yyyy-MM-dd.log
。
對於第二個日誌出現在C:\Logger.Name\yyyy-MM-dd.log
。
發生了什麼事?配置和啓用代碼是相同的,但日誌始終顯示在兩個不同的位置,一個是有意的,另一個是意外的。
對於我的生活,我不明白髮生了什麼事情。
在WCF中,'HttpContext.Current;'將總是** null ** ...所以'builder.Append'不會被調用...從[MSDN](http://msdn.microsoft .com/en-us/library/aa702682.aspx):HttpContext:當從WCF服務中訪問時,Current始終爲空。改用RequestContext。 – nemesv
我實際上已經排除了這個問題,但我會將你的評論標記爲對於HttpContext上的MSDN引用的回答在WCF中始終爲空。如果你喜歡,請添加你的答案作爲評論,讓我這樣做。 –