2016-04-18 56 views
0

我想用adoNetappender登錄到sql server,它與app.config一起工作,但我需要它whiteout一個app.config這是我的測試代碼爲adoNetappender。log4net沒有app.config

[assembly: log4net.Config.XmlConfigurator(Watch = true)] 

命名空間TestAdoNet {

class Program 
{ 
    private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 

    static void Main(string[] args) 
    { 
     Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); 

     PatternLayout patternLayout = new PatternLayout(); 
     patternLayout.ConversionPattern = "%date [%thread] %level %logger - %message%newline"; 
     patternLayout.ActivateOptions(); 

     AdoNetAppender adoNet = new AdoNetAppender(); 

     adoNet.BufferSize = 1; 
     adoNet.Layout = patternLayout; 
     adoNet.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; 
     adoNet.ConnectionString = "data source=.; initial catalog=test;integrated security=true;"; 
     adoNet.CommandText = "INSERT INTO Log2 ([Date],[Thread],[Level],[Logger], [Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"; 


     AdoNetAppenderParameter logDate = new AdoNetAppenderParameter(); 

     logDate.ParameterName = "@log_date"; 
     logDate.DbType = DbType.DateTime; 
     logDate.Layout = new RawTimeStampLayout(); 

     adoNet.AddParameter(logDate); 

     RawLayoutConverter rlc = new RawLayoutConverter(); 

     AdoNetAppenderParameter logThread = new AdoNetAppenderParameter(); 


     logThread.ParameterName = "@thread"; 
     logThread.DbType = DbType.String; 
     logThread.Size = 255; 
     logThread.Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%thread")); 

     adoNet.AddParameter(logThread); 

     AdoNetAppenderParameter logLevel = new AdoNetAppenderParameter(); 

     logLevel.ParameterName = "@log_level"; 
     logLevel.DbType = DbType.String; 
     logLevel.Size = 50; 
     logLevel.Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%level")); 

     adoNet.AddParameter(logLevel); 

     AdoNetAppenderParameter logLogger = new AdoNetAppenderParameter(); 

     logLogger.ParameterName = "@logger"; 
     logLogger.DbType = DbType.String; 
     logLogger.Size = 255; 
     logLogger.Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%logger")); 

     adoNet.AddParameter(logLogger); 

     AdoNetAppenderParameter logMessage = new AdoNetAppenderParameter(); 

     logMessage.ParameterName = "@message"; 
     logMessage.DbType = DbType.String; 
     logMessage.Size = 4000; 
     logMessage.Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%message")); 

     adoNet.AddParameter(logMessage); 

     AdoNetAppenderParameter logException = new AdoNetAppenderParameter(); 


     logException.ParameterName = "@exception"; 
     logException.DbType = DbType.String; 
     logException.Size = 2000; 
     logException.Layout = (IRawLayout)rlc.ConvertFrom(new ExceptionLayout()); 


     adoNet.ActivateOptions(); 
     hierarchy.Root.AddAppender(adoNet); 
     hierarchy.Root.Level = Level.All; 
     hierarchy.Configured = true; 


     logger.Error("test"); 

    } 
} 

我試圖與rolingfileappender相同,它的工作原理,現在我唯一的問題是在SQL Server。 我沒有收到任何錯誤消息,所以我不知道什麼是錯的。

+0

您需要[啓用log4net診斷日誌記錄](http://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems)看到任何調試消息log4net可能有輸出。 – stuartd

回答

0

感謝@stuartd我發現它

錯誤:ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;",

右:ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",

的 「;」在最後是問題:D