我正在使用 log4net appender ADO.NET 将 Azure Worker Role 的消息记录到 SQL Azure 实例(默认诊断不适合)。出于某种原因,在开发结构中运行工作程序时,日志记录工作。然而,当实例部署到云(具有完全相同的配置)时,不会记录错误。
使用此文件在代码中进行配置:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<renderer renderingClass="{ExceptionRenderer}" renderedClass="System.Exception" />
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<!--<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />-->
<conversionPattern value="%message%newline" />
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="3" />
<connectionType value="{ConnectionType}" />
<connectionString value="{ConnectionString}" />
<commandText value="INSERT INTO Salescast_Log ([Date],[Thread],[Version],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, '{Version}',@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="4000" />
<layout type="{ExceptionLayoutType}" />
</parameter>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="TraceAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
当 Autofac IoC 环境初始化时(每个角色启动),log4net 会使用正确的值初始化。代码如下所示:
static ILogProvider BuildProvider(IComponentContext context)
{
var connection = context
.Resolve<IProvideBusSettings>()
.GetString("SqlConnection")
.ExposeException("Failed to get SQL string for logging");
var xml = Properties.Resources.Logging
.Replace("{ConnectionType}", typeof(SqlConnection).AssemblyQualifiedName)
.Replace("{ConnectionString}", connection)
.Replace("{ExceptionLayoutType}", typeof(LoggingTrimmedExceptionLayout).AssemblyQualifiedName)
.Replace("{ExceptionRenderer}", typeof(LoggingExceptionRenderer).AssemblyQualifiedName)
.Replace("{Version}", SystemDescriptor.Default.Version.ToString());
var doc = new XmlDocument();
doc.LoadXml(xml);
XmlConfigurator.Configure(doc.DocumentElement);
return new LoggingProvider();
}
使用默认 Azure 操作系统。SQL 连接显然是有效的。
请任何人想一想为什么 log4net 从开发结构记录错误,但无法从 azure OS 使用完全相同的服务配置文件执行此操作的原因?