配置 log4net Entity Framework 数据库优先

Configure log4net Entity Framework database-first

我正在尝试在使用 Entity Framework 6.0 的项目中以数据库优先的方式实施 log4net。我可以登录到一个文件,我可以使用 ADO 创建一个单独的连接字符串,但我想使用 Entity Framework 上下文。有谁知道任何网站提供有关如何实现此目的的信息?

这是我当前用于记录到文件的配置:

<configSections> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="c:\logs\webAApp.log"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="2" />
      <maximumFileSize value="1MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
      </layout>
    </appender>
    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
    </root>
</log4net>

这是我的新配置:

<configSections>
<section name="log4net" ype="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="ADONetAppender" />
    </root>
    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="server=pcrcrm.database.windows.net; uid=pcrdevadmin; pwd=Priv@te1; database=CRM-dev" />
      <commandText value="INSERT INTO [dbo].[ERROR_LOGGING] ([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>
  </log4net>

这是你可以做到的

static void Main()
{
     XmlConfigurator.Configure();
     SetupLog4Net();
}

private static void SetupLog4Net()
{
    Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
    if(hierarchy != null && hierarchy.Configured)
    {
        foreach(IAppender appender in hierarchy.GetAppenders())
        {
           if(appender is AdoNetAppender)
           {
               var adoNetAppender = (AdoNetAppender)appender;
               adoNetAppender.ConnectionString = dbContext.Database.Connection.ConnectionString;
               adoNetAppender.ActivateOptions();
           }
        }
    }
}

希望对您有所帮助。

首先,创建一个日志class。

public class DatabaseLogger
{
    readonly static log4net.ILog logger = log4net.LogManager.GetLogger("ADONetAppender");

    public static void LogDebug(string message)
    {
        logger.Debug(message);
    }
}

创建一个继承自 EF 上下文的新上下文 class,并在构造函数内部将数据库日志上的 属性 设置为上述调试器 class 的一个实例。

public partial class InheritedContext : DbContext
{
    public InheritedContext() :
        base("DatabaseConnectionString")
    {
        Database.Log = DatabaseLogger.LogDebug;
        this.Configuration.LazyLoadingEnabled = false;
    }