0
我使用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;
}