gpt4 book ai didi

c# - 配置 log4net Entity Framework 数据库优先

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:19 25 4
gpt4 key购买 nike

我正尝试在一个使用 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();
}
}
}
}

希望这对您有所帮助。

关于c# - 配置 log4net Entity Framework 数据库优先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com