Log4net with Entity Framework 5 and MVC4

2019-05-07 13:28发布

问题:

So I am starting this new project and want to use Log4net to log to the Database. I am using EF for other things and it connects fine. For some reason L4N is not logging anything but not throwing any errors. Just wondering what step(s) I may be missing, hope someone can point it out. Do I need to "save" to the db somehow? Thanx so much in advance.

My web.config:

<log4net> 
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="100" />
  <connectionType value="System.Data.EntityClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="data source=myserver;initial catalog=Reporting;integrated security=false;persist security info=True;User ID=myid;Password=mypassword" />
  <commandText value="INSERT INTO [myschema].[Log] ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%thread" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%logger" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>
</appender>
<root>
  <level value="Info" />
  <appender-ref ref="LogFileAppender" />
</root>

and my controller:

 ...
 using log4net;
 using System.Reflection;

namespace MyNames.Controllers
{
  public class ClientController : Controller
  {    
     ReportingEntities db = new ReportingEntities();
     private readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

     public ActionResult Index()
     {  
         this.log.Debug("Sample message to log to Db");
         return View();
     }
   }
 }

EDIT: So I made the mods suggested gorilly but still doesnt work. Just happened to check output window and even though I enabled debugging it still shows this .. Tried the debugging suggestion from here but no error was thrown.

From my log4net internal debugging file:

log4net: Assembly [DotNetOpenAuth.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246] Loaded From [C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\bf082a83\4eb20e9a\assembly\dl3\c62a3103\e01aa9f6_fda1cf01\DotNetOpenAuth.Core.dll]
log4net: Assembly [DotNetOpenAuth.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246] does not have a RepositoryAttribute specified.
log4net: Assembly [DotNetOpenAuth.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=2780ccd10d57b246] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Shutdown called on Hierarchy [log4net-default-repository]

回答1:

Add this line to Global.asax.cs (in Application_Start()):

log4net.Config.XmlConfigurator.Configure();

Also, you can turn on log4net debugging to help you figure out what's going wrong by adding this line to your Web.config under <appSettings>:

Edit: The below had value="false", but should be value="true".

<add key="log4net.Internal.Debug" value="true"/>

Edit:

You can send log4net's internal debug logging to a file by adding the following to your Web.config (see the official explanation here):

  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add
            name="textWriterTraceListener"
            type="System.Diagnostics.TextWriterTraceListener"
            initializeData="C:\mylogdirectory\log4net_debug.txt" />
      </listeners>
    </trace>
  </system.diagnostics>