我使用 Log4Net 进行日志记录。当应用程序启动时,我打电话
log4net.Config.XmlConfigurator.Configure();
但是这条线需要 15 秒才能完成。难道我做错了什么?还是正常?
我正在使用 ASP.NET MVC 进行开发,使用 Unity 进行依赖注入。
在应用程序启动时,我调用 Bootstrapper Initialise 函数
protected void Application_Start()
{
IUnityContainer container = Bootstrapper.Initialise();
...
...
}
在 Bootstrapper Initialize 函数中,我注册了 ILog 类型。
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
...
...
container.RegisterType<ILog>("", new ContainerControlledLifetimeManager(),
new InjectionFactory(factory =>
LogManager.GetLogger(typeof(HomeController).Assembly, connectionString)));
...
...
}
在 GetLogger 函数的开头,我调用了 configure 函数
public static ILog GetLogger(Assembly assembly, string connectionString)
{
log4net.Config.XmlConfigurator.Configure(); //<----- it takes 15 seconds to finish
...
...
}
编辑
------------------------------------------------- --------------------------------
<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="0" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[UserId],[Operation],[EntityType],[EntityId],[IP],[Host],[SessionId],[LogGroup]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @UserId, @Operation, @EntityType, @EntityId, @IP, @Host, @SessionId, @LogGroup)" />
<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>
<parameter>
<parameterName value="@UserId"/>
<dbType value="Int32" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="UserId" />
</layout>
</parameter>
<parameter>
<parameterName value="@IP"/>
<dbType value="String" />
<size value="25" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="IP" />
</layout>
</parameter>
<parameter>
<parameterName value="@Host"/>
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="Host" />
</layout>
</parameter>
<parameter>
<parameterName value="@LogGroup"/>
<dbType value="Int32" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="LogGroup" />
</layout>
</parameter>
<parameter>
<parameterName value="@Operation"/>
<dbType value="Int32" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="Operation" />
</layout>
</parameter>
<parameter>
<parameterName value="@EntityType"/>
<dbType value="Int32" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="EntityType" />
</layout>
</parameter>
<parameter>
<parameterName value="@EntityId"/>
<dbType value="Int32" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="EntityId" />
</layout>
</parameter>
<parameter>
<parameterName value="@SessionId"/>
<dbType value="String" />
<size value="88" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="SessionId" />
</layout>
</parameter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>