5

我有一些使用log4net沒有問題的ASP.NET應用程序。我無法讓log4net記錄任何使用MVC庫編寫的任何服務。所有服務中的配置都相同(除日誌文件名外)。每個服務都以相同的用戶身份運行並使用相同的應用程序池。應用程序不記錄

這裏是我使用的配置:

<log4net> 
     <appender name="XMLAppender" type="log4net.Appender.RollingFileAppender"> 
      <file value="C:\logs\SERVICENAME.Log.xml"/> 
      <appendToFile value="true"/> 
      <rollingStyle value="Composite"/> 
      <datePattern value="yyyyMM"/> 
      <maxSizeRollBackups value="10"/> 
      <maximumFileSize value="100MB"/> 
      <layout type="log4net.Layout.XmlLayoutSchemaLog4j"> 
       <locationInfo value="false"/> 
      </layout> 
    </appender> 
     <root> 
      <level value="INFO"/> 
      <appender-ref ref="XMLAppender"/> 
     </root> 
    </log4net> 

我在所有的configSection項,如下所示:

<configSections> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 
... 
</configSections> 

我也打開log4net的調試和設置System.Diagnostics程序來將跟蹤寫入文件。這裏是我看到MVC和非MVC應用程序之間的輸出差異。

MVC:

log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\MVCSERVICENAME\25c1b478\883641ac\assembly\dl3\707a9005\e1887c7f_5040ca01\log4net.DLL]. (.NET Runtime [2.0.50727.3074] on Microsoft Windows NT 6.0.6002 Service Pack 2, v.286) 
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy] 
log4net: DefaultRepositorySelector: Creating repository for assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] 
log4net: DefaultRepositorySelector: Assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\MVCSERVICENAME\25c1b478\883641ac\assembly\dl3\13a19b30\fdb642f4_937dca01\MVCSERVICENAME.DLL] 
log4net: DefaultRepositorySelector: Assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified. 
log4net: DefaultRepositorySelector: Assembly [MVCSERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy] 
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy] 
log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository] 

非MVC(工作):

log4net log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\SERVICENAME\33034378\c825df91\assembly\dl3\fe97001a\e1887c7f_5040ca01\log4net.DLL]. (.NET Runtime [2.0.50727.3074] on Microsoft Windows NT 6.0.6002 Service Pack 2, v.286) 
log4net DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy] 
log4net DefaultRepositorySelector: Creating repository for assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] 
log4net DefaultRepositorySelector: Assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\SERVICENAME\33034378\c825df91\assembly\dl3\827ed068\1a38e2eb_fc7cca01\SERVICENAME.DLL] 
log4net DefaultRepositorySelector: Assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified. 
log4net DefaultRepositorySelector: Assembly [SERVICENAME, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy] 
log4net DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy] 
log4net XmlConfigurator: configuring repository [log4net-default-repository] using .config file section 
log4net XmlConfigurator: Application config file is [C:\content\SERVICENAME\web.config] 
log4net XmlConfigurator: Configuring Repository [log4net-default-repository] 
log4net XmlHierarchyConfigurator: Configuration update mode [Merge]. 
log4net XmlHierarchyConfigurator: Logger [root] Level string is [INFO]. 
log4net XmlHierarchyConfigurator: Logger [root] level set to [name="INFO",value=40000]. 
log4net XmlHierarchyConfigurator: Loading Appender [XMLAppender] type: [log4net.Appender.RollingFileAppender] 
log4net XmlHierarchyConfigurator: Setting Property [File] to String value [C:\Windows\Temp\SERVICENAME.Log.xml] 
log4net XmlHierarchyConfigurator: Setting Property [AppendToFile] to Boolean value [True] 
log4net XmlHierarchyConfigurator: Setting Property [RollingStyle] to RollingMode value [Composite] 
log4net XmlHierarchyConfigurator: Setting Property [DatePattern] to String value [yyyyMM] 
log4net XmlHierarchyConfigurator: Setting Property [MaxSizeRollBackups] to Int32 value [10] 
log4net XmlHierarchyConfigurator: Setting Property [MaximumFileSize] to String value [100MB] 
log4net XmlHierarchyConfigurator: Setting Property [LocationInfo] to Boolean value [False] 
log4net XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.XmlLayoutSchemaLog4j] 
log4net RollingFileAppender: Type = [0], r0 = [197001], r1 = [197001] 
log4net RollingFileAppender: Type = [1], r0 = [197001], r1 = [197001] 
log4net RollingFileAppender: Type = [2], r0 = [197001], r1 = [197001] 
log4net RollingFileAppender: Type = [3], r0 = [197001], r1 = [197001] 
log4net RollingFileAppender: Type = [4], r0 = [197001], r1 = [197001] 
log4net RollingFileAppender: Type = [5], r0 = [197001], r1 = [197002] 
log4net RollingFileAppender: Searched for existing files in [C:\Windows\Temp] 
log4net RollingFileAppender: curSizeRollBackups starts at [0] 
log4net RollingFileAppender: [200912] vs. [200912] 
log4net FileAppender: Opening file for writing [C:\Windows\Temp\SERVICENAME.Log.xml] append [True] 
log4net XmlHierarchyConfigurator: Created Appender [XMLAppender] 
log4net XmlHierarchyConfigurator: Adding appender named [XMLAppender] to logger [root]. 
log4net XmlHierarchyConfigurator: Hierarchy Threshold [] 
log4net DefaultRepositorySelector: Creating repository for assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] 
log4net DefaultRepositorySelector: Assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\SERVICENAME\33034378\c825df91\assembly\dl3\d74dc9d5\8aa6a7e8_fc7cca01\SERVICENAME.Core.DLL] 
log4net DefaultRepositorySelector: Assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified. 
log4net DefaultRepositorySelector: Assembly [SERVICENAME.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy] 
log4net DefaultRepositorySelector: repository [log4net-default-repository] already exists, using repository type [log4net.Repository.Hierarchy.Hierarchy] 

在這兩種情況下,我創建了ILog的對象,如下所示:

私人靜態只讀ILog的日誌=日誌管理.GetLogger(typeof運算(CLASSNAME));

有什麼我失蹤或我需要做一些改變,以獲得這項工作的MVC?此時,如果已知能夠正確使用MVC,我也願意更改爲另一種日誌記錄解決方案。

感謝 添

+0

您的工作進程是否具有對c:\ logs的寫入權限? – 2009-12-15 16:57:08

+0

是的。對於它的價值,我只是試過NLog,它的工作完美。前段時間應該已經放棄了log4net。 – 2009-12-15 17:21:27

回答

0

使用NLOG。似乎工作。我不得不把NLog.config文件放在基本目錄(web.config旁邊),它工作正常。能夠寫入正確的目錄,這證明目錄權限已正確設置。花了大約5分鐘開始完成改變。

很高興知道爲什麼log4net拒絕播放。

+1

或查看ELMAH http://code.google.com/p/elmah/ – David 2009-12-15 17:32:24

+0

我會研究它。謝謝大衛。 – 2009-12-16 14:10:26

+2

ELMAH非常好,但它不會取代log4net,它補充了它。 ELMAH是記錄未處理的異常的好資源,例如由於某種原因滑過你的lo4net調用的東西(例如開發人員忘記添加try/catch或記錄異常) – 2010-10-05 18:53:17

10

我知道這個職位是很老,但我得到一個WCF項目還這個問題(默認log4net的庫)。 我正在使用web.config和log4net部分(而不是單獨的log4net.config文件)。

爲了解決我放在我的AssemblyInfo.cs以下問題:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)] 

同樣的,如果使用外部配置文件,雖然也是如此。 HTH, 夏蘭

+0

Thankyou - 我有完全相同的問題,這解決了它。 – Justin 2011-02-21 00:12:12

+0

我在app_statup中使用代碼,使用它代替解決了問題。 – Clint 2017-10-04 14:35:34

0

在我的情況,這是因爲項目屬性分別設置爲輸出到BIN \ debug,但log4net的DLL所需的log4net.config文件中的BIN文件夾中存在。我將項目屬性更改爲輸出到bin文件夾,然後在構建時將log4net.config文件複製到正確的位置。

0

此外,確保爲log4net配置文件設置「copy local」也會防止出現很多問題。

+0

這應該可能是一個評論,而不是一個完整的答案... – IronMan84 2012-12-26 22:13:18

4

我的web應用程序是一個ASP.NET MVC,在web根目錄下有一個單獨的log4net.config文件。

經過大量谷歌搜索和拔頭髮的我使出通過代碼初始化log4net的的Application_Start(Global.asax中):

protected void Application_Start() 
{ 
    var logConfigFilePath = Server.MapPath("~/log4net.config"); 

    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logConfigFilePath)); 
} 

這解決了這個問題,我和log4net的正確啓動日誌記錄。

+0

注意,你可以把log4net配置放在'Web.config'中。使用'Server.MapPath(「〜/ Web.config」);'似乎將XmlConfigurator指向該文件。 – 2014-01-05 21:38:55