Log4net在ASP.NET MVC应用程序中不起作用

Web.config文件

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
  </configSections>
  <log4net configSource="configlog4net.config" />
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <connectionStrings>
    <add name="catalogiDbName" connectionString="Data Source=dbserver;Initial Catalog=Catalogi;User ID=sa;Password=pyrAmid09" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5.1" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>     
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

配置 log4net.config

<?xml version="1.0"?>
<log4net>

  <root>
    <priority value="Debug"/>
    <appender-ref ref="AsyncLog4NetAppender" />
  </root>

  <appender name="rollingFile" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="App_DataLogsCatalogiTraceLog.%property{log4net:HostName}.txt" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <maximumFileSize value="5MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value=" %date [P%property{processId}/D%property{appDomainId}/T%thread] %-5level %logger - %message%newline" />
    </layout>
    <encoding value="utf-8" />
  </appender>

  <appender name="AsyncLog4NetAppender" type="Log4Net.Async.ParallelForwardingAppender">
    <appender-ref ref="rollingFile" />
  </appender>

</log4net>

我的web项目的AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Catalogi.Web.UI")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Catalogi.Web.UI")]
[assembly: AssemblyCopyright("Copyright ©  2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("df78b702-d445-4a44-8d80-2888056184b2")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

// Log4net
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

我试图解决这个问题时尝试了很多东西:

  • 添加路径到配置文件的程序集中

    [程序集:log4net.Config.XmlConfigurator(ConfigFile =“Config log4net.config”,Watch = true)]

  • 将副本设置为log4net.config文件的输出

  • 在web配置中添加<add key="log4net.Internal.Debug" value="true"/>以查看调试

    log4net:log4net程序集[log4net,Version = 2.0.8.0,Culture = neutral,PublicKeyToken = 669e0ddf0bb1aa2a]。 从[C: Users MichaelV.PROSEC AppData Local Temp Temporary ASP.NET Files root cd31ee63 e12ae4ed assembly dl3 4cb568d0 dbc1e838_1decd201 log4net.dll]加载。 (Microsoft Windows NT 6.3.9600.0上的.NET Runtime [4.0.30319.42000])

    log4net:defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]

    log4net:为程序集创建存储库[Catalogi.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]

    log4net:Assembly [Catalogi.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]从[C: Users MichaelV.PROSEC AppData Local Temp Temporary ASP.NET Files root cd31ee63 e12ae4ed 组件 DL3 c63084c3 0184b750_0cf4d201 Catalogi.Core.dll]

    log4net:程序集[Catalogi.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]没有指定RepositoryAttribute。

    使用存储库[log4net-default-repository]和存储库类型[log4net.Repository.Hierarchy.Hierarchy]的log4net:Assembly [Catalogi.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]

    log4net:使用类型[log4net.Repository.Hierarchy.Hierarchy]创建存储库[log4net-default-repository]


  • 在这个问题上挣扎后,我找到了解决办法。

    首先,我必须添加一个新的appsettings键来查找分离的配置文件:

    <add key="log4net.Config" value="configlog4net.config"/>
    

    之后,我开始在输出控制台中看到更多信息。 我必须先为我的Log4Net.Async.ParallelForwardingAppender添加软件包log4net.async,之后我必须将以下内容添加到Web配置中,因为它无法加载先前版本的log4net:

      <dependentAssembly>
        <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.8.0" newVersion="2.0.8.0"/>
      </dependentAssembly>
    

    现在一切正常。

    链接地址: http://www.djcxy.com/p/43661.html

    上一篇: Log4net not working in a ASP.NET MVC Application

    下一篇: log4net doesnt work when project is set to Release build