|
> log4netを使用してSQLServerのテーブルへログを書き込みしたいのですが > log4net.configファイルに下記の記述しデバック実行するとデバッグ箇所に > 「log4net:ERROR XmlHierarchyConfigurator: No appender named [AdoNetAppender] could be found.」 > 「log4net:ERROR XmlHierarchyConfigurator: Appender named [AdoNetAppender] not found.」 > が出力されSQLServerへログの書き込みができません。 > 何が原因か判明しましたらご教授お願い致します。
その通りなんですが・・・
宣言は、『ADONetAppender』となっていますが、『AdoNetAppender』と指定して いますので、『not Found』と・・・
大文字・小文字の区別がありますので、正確に記述して下さい。
※ 尚、log4net.config や、AssemblyInfo.cs に記載しなくても、プログラムのコード中 で、すべて設定できます。
たとえば・・・
Public Function OpenLog4netDB(ByVal Server As String, ByVal DbName As String, Optional ByVal bSecurity As Boolean = True, Optional ByVal User As String = "", Optional ByVal Pass As String = "", Optional ByVal Timeout As Integer = 5) As Boolean
Dim h As Hierarchy = CType(LogManager.GetRepository, Hierarchy)
h.LoggerFactory.CreateLogger("logger")
Dim logger As Logger
logger = h.GetLogger("logger", h.LoggerFactory)
Dim layout As log4net.Layout.PatternLayout = New log4net.Layout.PatternLayout
layout.ConversionPattern = "%d %-5p - %m%n" layout.ActivateOptions()
Dim Para As AdoNetAppenderParameter Dim appender As AdoNetAppender = New AdoNetAppender
With appender .Name = "Database" .ConnectionType = "System.Data.SqlClient.SqlConnection" .ConnectionString = GetDBConnectString(Server, DbName, bSecurity, User, Pass, Timeout) .CommandType = CommandType.Text .CommandText = "INSERT INTO LOG_DATA ([Date],[Level],[Message]) VALUES (@log_date, @log_level, @message)" .Layout = layout .Threshold = Core.Level.All .BufferSize = 100
Para = New AdoNetAppenderParameter With Para .ParameterName = "@log_date" .DbType = DbType.DateTime .Layout = New Layout.RawTimeStampLayout appender.AddParameter(Para) End With
Para = New AdoNetAppenderParameter With Para .ParameterName = "@log_level" .DbType = DbType.String .Size = 50 .Layout = New Layout.Layout2RawLayoutAdapter(New Layout.PatternLayout("%p")) appender.AddParameter(Para) End With
Para = New AdoNetAppenderParameter With Para .ParameterName = "@message" .DbType = DbType.String .Size = 4000 .Layout = New Layout.Layout2RawLayoutAdapter(New Layout.PatternLayout("%m")) appender.AddParameter(Para) End With
.ActivateOptions() End With
logger.AddAppender(appender) logger.Level = Level.All
BasicConfigurator.Configure(h, appender)
log4 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
bOpen = True
Return bOpen
End Function
Public Function Close() As Boolean
If bOpen Then log4net.LogManager.Shutdown() End If
bOpen = False
Return True
End Function
Private Function GetDBConnectString(ByVal Server As String, ByVal DbName As String, ByVal bSecurity As Boolean, ByVal User As String, ByVal Pass As String, ByVal Timeout As Integer) As String
Dim AdoSQL As SqlConnectionStringBuilder
AdoSQL = New SqlConnectionStringBuilder
With AdoSQL .DataSource = Server .InitialCatalog = DbName .IntegratedSecurity = bSecurity .Password = Pass .UserID = User .ConnectTimeout = Timeout End With
Return AdoSQL.ToString
End Function
あくまで参考です。ご自身の環境用に変更しないと動作しないでしょう。 ログを保存するDBのテーブル構造も・・・です。
以上。
|