2013-04-23 60 views
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; 
} 

回答

0

我想清楚是怎麼回事。爲什麼我總是在消息的原因是因爲它是使用默認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")) 
     }); 
相關問題