Log4Net数据库存储 - 所有字段都包含消息值
问题描述:
我使用AdoNetAppender激活log4net以将日志存储在Sql Server数据库中。Log4Net数据库存储 - 所有字段都包含消息值
执行后,我看到所有参数(DateTime除外)都填充了消息值。
我自己在代码中创建适配器(没有xml配置),所以我假设可能有一种方法我忘记调用以使模式映射工作?
var root = ((Hierarchy)LogManager.GetRepository()).Root;
root.AddAppender(this.CreateDBAppender());
root.Repository.Configured = true;
this.log = LogManager.GetLogger("MyLogger");
private IAppender CreateDBAppender()
{
...
RawLayoutConverter layoutConverter = new RawLayoutConverter();
var databaseAppender = new AdoNetAppender
{
Name = "MyLogger",
ConnectionString = "...",
CommandText = @"INSERT INTO [Log] ([DateTime], [Exception], [Host], [Level], [Logger], [Message], [Severity], [Thread])
VALUES (@logdate, @exception, @host, @level, @logger, @message, @severity, @thread)",
CommandType = System.Data.CommandType.Text,
ConnectionType = "System.Data.SqlClient.SqlConnection"
};
databaseAppender.AddParameter(new AdoNetAppenderParameter
{
ParameterName = "@logdate",
DbType = System.Data.DbType.DateTime,
Layout = new RawTimeStampLayout()
});
databaseAppender.AddParameter(new AdoNetAppenderParameter
{
ParameterName = "@message",
DbType = System.Data.DbType.String,
Size = 4000,
Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout
{
ConversionPattern = "%m"
})
});
databaseAppender.AddParameter(new AdoNetAppenderParameter
{
ParameterName = "@host",
DbType = System.Data.DbType.String,
Size = 50,
Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout
{
ConversionPattern = "%property{host}"
})
});
databaseAppender.AddParameter(new AdoNetAppenderParameter
{
ParameterName = "@level",
DbType = System.Data.DbType.String,
Size = 50,
Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout
{
ConversionPattern = "%level"
})
});
//// more parameters ...
databaseAppender.ActivateOptions();
return databaseAppender;
}
答
我想清楚是怎么回事。为什么我总是在消息的原因是因为它是使用默认ConversionPattern作为的PatternLayout类
public const string DefaultConversionPattern = "%message%newline";
它看起来像设置ConversionPattern财产代码没有任何影响配置。 您需要通过PatternLayout类的构造函数提供该模式。
databaseAppender.AddParameter(new AdoNetAppenderParameter
{
ParameterName = "@level",
DbType = System.Data.DbType.String,
Size = 50,
Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout("%-5p"))
});