我正在使用log4net appender ADO.NET将Azure Worker Role的消息记录到sql Azure实例(默认诊断不适合).出于某种原因,在开发结构中运行worker时,日志记录工作.然而,当实例部署到云(具有完全相同的配置)时,错误不会被记录.
<?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 OS. sql连接显然是有效的.
请问任何人,想一想原因,为什么log4net会从开发结构中记录错误,但是使用完全相同的服务配置文件却无法从azure OS中执行此操作?
解决方法
我刚刚遇到这个问题并花了一天的时间来试图解决这个问题.归结为sql Azure需要在表上使用聚簇索引. log4net提供的用于创建Log表的示例sql代码没有聚集索引,这是sql Azure的要求.除非它具有聚集索引(截至编写时),否则将任何数据添加到表中都将失败.
尝试在连接到sql Azure时使用sql Server Management Studio执行手动插入语句,如果这是问题,它会立即告诉您.如果是这样,请运行以下sql以在表上添加聚簇索引(假设您使用了log4net中的sql直接),然后再次尝试.
CREATE UNIQUE CLUSTERED INDEX PK_Log ON [Log] ([Id]) GO