2016-02-29 36 views
0

我對log4net有疑問。如何在C#中使用ADOAppender類。我必須捕獲數據庫中的日誌。我已經嘗試通過在配置文件中配置。並且工作正常。我需要在代碼中配置它。如何在沒有配置的log4net中使用AdoAppender

我已經通過以下code.And試圖不工作

 Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); 

     MemoryAppender memory = new MemoryAppender(); 
     AdoNetAppender adoappender = new AdoNetAppender(); 
     AdoNetAppenderParameter adoapppar = new AdoNetAppenderParameter(); 
     AdoNetAppenderParameter adoapppar1 = new AdoNetAppenderParameter(); 
     AdoNetAppenderParameter adoapppar2 = new AdoNetAppenderParameter(); 
     AdoNetAppenderParameter adoapppar3 = new AdoNetAppenderParameter(); 
     AdoNetAppenderParameter adoapppar4 = new AdoNetAppenderParameter(); 
     AdoNetAppenderParameter adoapppar5 = new AdoNetAppenderParameter(); 

     PatternLayout patternLayout = new PatternLayout(); 

     adoappender.ConnectionString = "Data Source=.;Initial Catalog=xmltest;Integrated Security=false;User ID=sa;Password=dbadmin;"; 
     adoappender.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; 
     adoappender.ReconnectOnError = true; 

     adoappender.CommandType = CommandType.StoredProcedure; 
     adoappender.CommandText = "DBO.splog4nettest"; 
     adoappender.BufferSize = 1; 

     adoapppar.ParameterName = "@log_date"; 
     adoapppar.DbType = DbType.DateTime; 
     adoapppar.Layout = new Layout2RawLayoutAdapter(patternLayout); 
     patternLayout.ConversionPattern = "%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}"; 
     patternLayout.ActivateOptions(); 
     adoappender.AddParameter(adoapppar); 

     adoapppar1.ParameterName = "@thread"; 
     adoapppar1.DbType = DbType.String; 
     adoapppar1.Size = 255; 
     adoapppar1.Layout = new Layout2RawLayoutAdapter(patternLayout); 
     //adoapppar.Layout = (IRawLayout)patternLayout; 
     patternLayout.ConversionPattern = "%thread"; 
     patternLayout.ActivateOptions(); 
     adoappender.AddParameter(adoapppar1); 

     adoapppar2.ParameterName = "@log_level"; 
     adoapppar2.DbType = DbType.String; 
     adoapppar2.Size = 50; 
     adoapppar2.Layout = new Layout2RawLayoutAdapter(patternLayout); 
     //adoapppar.Layout = (IRawLayout)patternLayout; 
     patternLayout.ConversionPattern = "%level"; 
     patternLayout.ActivateOptions(); 

     memory.ActivateOptions(); 
     hierarchy.Root.AddAppender(memory); 

     hierarchy.Root.Level = Level.Info; 
     hierarchy.Configured = false; 

     log4net.Config.BasicConfigurator.Configure(); 
+0

什麼是不工作? – Jacobr365

+0

我無法更新表中的日誌 – Panchi

+0

您是否收到錯誤消息?你有沒有試過調試它?在appender的配置文件中將此行添加到您的應用程序設置中可讓您調試log4net。 'add key =「log4net.Internal.Debug」value =「true」>' – Jacobr365

回答

0

你還沒有添加AdoAppender紮根

hierarchy.Root.AddAppender(adoappender);