Log4net无法在ASP.NET MVC应用程序中运行

Web.config

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />
  </configSections>
  <log4net configSource="config\log4net.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>

Config\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_Data\Logs\CatalogiTraceLog.%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>

AssemblyInfo.cs of my web project

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)]

我试图解决这个问题尝试了很多方面:

  • 在程序集中添加配置文件的路径,如

[assembly:log4net.Config.XmlConfigurator(ConfigFile =“Config \ log4net.config”,Watch = true)]

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

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

log4net:log4net assembly [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运行时[4.0.30319.42000])

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

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

log4net:程序集[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:程序集[Catalogi.Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]使用repository [log4net-default-repository]和存储库类型[log4net.Repository.Hierarchy.Hierarchy]

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

回答(1)

2 years ago

经过一番努力,我找到了解决方案 .

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

<add key="log4net.Config" value="config\log4net.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>

现在一切正常 .