类库中的 C# log4net
C# log4net in ClassLibrary
我是编程新手,正在尝试为项目设置日志记录 (log4net) 工具。我在设置一个 ClassLibrary 时遇到问题。我不确定我必须如何编写我的代码才能正常工作。
我通过 "Manage NuGet Packages..." 在我的 ClassLibrary 中安装了 log4net,app.config 也设置正确。
它适用于其他测试程序,但我需要将它用于多个项目,因此我尝试将它与 ClassLibrary 一起使用。就像我说的,我是编程新手,不知道如何使用 类、ClassLibrarys 等
谁能给我一个示例文件或类似的东西,这样我就可以看看它是如何工作的。
对于初学者,请尝试 Log4Net
并存储登录 database。这两个示例将帮助您了解 log4Net 以及如何使用它。谢谢
每次我在使用 Log4Net 时遇到问题(也就是每次我不得不使用它时),我总是最终使用本教程。非常清晰易懂。
我自己发现的 听说是我的密码 l
图书馆:
using System;
using System.Data;
using log4net;
using log4net.Repository.Hierarchy;
using log4net.Core;
using log4net.Appender;
using log4net.Layout;
[assembly: log4net.Config.XmlConfigurator( Watch = true )]
namespace log
{
public class LoggerLib
{
int i = 0;
public log4net.ILog logger;
public void DeclareClass( System.Type className ) { logger = log4net.LogManager.GetLogger( className ); }
public void LogError(string content, string customContent1)
{
log4net.LogicalThreadContext.Properties["CustomColumn1"] = customContent1;
logger.Error( content );
}
public void LoggerSetup(string SQLServer, string SQLDatenBank, string SQLTable, string customContent2)
{
if ( i == 0 )
{
RawLayoutConverter rlc = new RawLayoutConverter();
AdoNetAppender adoNet = new AdoNetAppender();
AdoNetAppenderParameter logDate = new AdoNetAppenderParameter();
AdoNetAppenderParameter thread = new AdoNetAppenderParameter();
AdoNetAppenderParameter logLevel = new AdoNetAppenderParameter();
AdoNetAppenderParameter logLogger = new AdoNetAppenderParameter();
AdoNetAppenderParameter message = new AdoNetAppenderParameter();
AdoNetAppenderParameter exception = new AdoNetAppenderParameter();
AdoNetAppenderParameter customColoumn1 = new AdoNetAppenderParameter();
AdoNetAppenderParameter customColoumn2 = new AdoNetAppenderParameter();
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.AddAppender( adoNet );
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
log4net.Config.BasicConfigurator.Configure( adoNet );
//logDate
logDate.ParameterName = "@log_date";
logDate.DbType = System.Data.DbType.DateTime;
logDate.Layout = new RawTimeStampLayout();
//thread
thread.ParameterName = "@thread";
thread.DbType = System.Data.DbType.String;
thread.Size = 255;
thread.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%thread" ) );
//logLevel
logLevel.ParameterName = "@log_level";
logLevel.DbType = System.Data.DbType.String;
logLevel.Size = 50;
logLevel.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%level" ) );
//logLogger
logLogger.ParameterName = "@logger";
logLogger.DbType = System.Data.DbType.String;
logLogger.Size = 255;
logLogger.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%logger" ) );
//message
message.ParameterName = "@message";
message.DbType = System.Data.DbType.String;
message.Size = 4000;
message.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%message" ) );
//exception
exception.ParameterName = "@exception";
exception.DbType = System.Data.DbType.String;
exception.Size = 2000;
exception.Layout = (IRawLayout)rlc.ConvertFrom( new ExceptionLayout() );
//customColoumn1
customColoumn1.ParameterName = "@customValue1";
customColoumn1.DbType = System.Data.DbType.String;
customColoumn1.Size = 2000;
customColoumn1.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%property{CustomColumn1}" ) );
//customColoumn2
customColoumn2.ParameterName = "@customValue2";
customColoumn2.DbType = System.Data.DbType.String;
customColoumn2.Size = 2000;
customColoumn2.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%property{CustomColumn2}" ) );
adoNet.BufferSize = 1;
adoNet.CommandType = System.Data.CommandType.Text;
adoNet.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
adoNet.ConnectionString = "data source=" + SQLServer + "; initial catalog=" + SQLDatenBank + ";integrated security=true";
adoNet.CommandText = "INSERT INTO " + SQLTable + " ([Date],[Thread],[Level],[Logger],[Message],[Exception],[CustomColoumn1],[CustomColoumn2]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @customValue1, @customValue2)";
adoNet.AddParameter( logDate );
adoNet.AddParameter( thread );
adoNet.AddParameter( logLevel );
adoNet.AddParameter( logLogger );
adoNet.AddParameter( message );
adoNet.AddParameter( exception );
adoNet.AddParameter( customColoumn1 );
adoNet.AddParameter( customColoumn2 );
adoNet.ActivateOptions();
log4net.LogicalThreadContext.Properties["CustomColumn2"] = customContent2;
i = 1;
}
else
{
log4net.LogicalThreadContext.Properties["CustomColumn2"] = customContent2;
}
}
}
}
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log;
namespace ConsoleApplication1
{
class Program
{
static System.Type className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
static void Main( string[] args )
{
LoggerLib loggerLib = new LoggerLib();
loggerLib.DeclareClass( className );
loggerLib.LoggerSetup(".","test","Log4", "TestColoum");
loggerLib.LogError( "TEXT5", "test4" );
loggerLib.LogError( "TEXT3", "test3" );
}
}
}
我是编程新手,正在尝试为项目设置日志记录 (log4net) 工具。我在设置一个 ClassLibrary 时遇到问题。我不确定我必须如何编写我的代码才能正常工作。
我通过 "Manage NuGet Packages..." 在我的 ClassLibrary 中安装了 log4net,app.config 也设置正确。 它适用于其他测试程序,但我需要将它用于多个项目,因此我尝试将它与 ClassLibrary 一起使用。就像我说的,我是编程新手,不知道如何使用 类、ClassLibrarys 等
谁能给我一个示例文件或类似的东西,这样我就可以看看它是如何工作的。
对于初学者,请尝试 Log4Net
并存储登录 database。这两个示例将帮助您了解 log4Net 以及如何使用它。谢谢
每次我在使用 Log4Net 时遇到问题(也就是每次我不得不使用它时),我总是最终使用本教程。非常清晰易懂。
我自己发现的 听说是我的密码 l
图书馆:
using System;
using System.Data;
using log4net;
using log4net.Repository.Hierarchy;
using log4net.Core;
using log4net.Appender;
using log4net.Layout;
[assembly: log4net.Config.XmlConfigurator( Watch = true )]
namespace log
{
public class LoggerLib
{
int i = 0;
public log4net.ILog logger;
public void DeclareClass( System.Type className ) { logger = log4net.LogManager.GetLogger( className ); }
public void LogError(string content, string customContent1)
{
log4net.LogicalThreadContext.Properties["CustomColumn1"] = customContent1;
logger.Error( content );
}
public void LoggerSetup(string SQLServer, string SQLDatenBank, string SQLTable, string customContent2)
{
if ( i == 0 )
{
RawLayoutConverter rlc = new RawLayoutConverter();
AdoNetAppender adoNet = new AdoNetAppender();
AdoNetAppenderParameter logDate = new AdoNetAppenderParameter();
AdoNetAppenderParameter thread = new AdoNetAppenderParameter();
AdoNetAppenderParameter logLevel = new AdoNetAppenderParameter();
AdoNetAppenderParameter logLogger = new AdoNetAppenderParameter();
AdoNetAppenderParameter message = new AdoNetAppenderParameter();
AdoNetAppenderParameter exception = new AdoNetAppenderParameter();
AdoNetAppenderParameter customColoumn1 = new AdoNetAppenderParameter();
AdoNetAppenderParameter customColoumn2 = new AdoNetAppenderParameter();
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Root.AddAppender( adoNet );
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
log4net.Config.BasicConfigurator.Configure( adoNet );
//logDate
logDate.ParameterName = "@log_date";
logDate.DbType = System.Data.DbType.DateTime;
logDate.Layout = new RawTimeStampLayout();
//thread
thread.ParameterName = "@thread";
thread.DbType = System.Data.DbType.String;
thread.Size = 255;
thread.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%thread" ) );
//logLevel
logLevel.ParameterName = "@log_level";
logLevel.DbType = System.Data.DbType.String;
logLevel.Size = 50;
logLevel.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%level" ) );
//logLogger
logLogger.ParameterName = "@logger";
logLogger.DbType = System.Data.DbType.String;
logLogger.Size = 255;
logLogger.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%logger" ) );
//message
message.ParameterName = "@message";
message.DbType = System.Data.DbType.String;
message.Size = 4000;
message.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%message" ) );
//exception
exception.ParameterName = "@exception";
exception.DbType = System.Data.DbType.String;
exception.Size = 2000;
exception.Layout = (IRawLayout)rlc.ConvertFrom( new ExceptionLayout() );
//customColoumn1
customColoumn1.ParameterName = "@customValue1";
customColoumn1.DbType = System.Data.DbType.String;
customColoumn1.Size = 2000;
customColoumn1.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%property{CustomColumn1}" ) );
//customColoumn2
customColoumn2.ParameterName = "@customValue2";
customColoumn2.DbType = System.Data.DbType.String;
customColoumn2.Size = 2000;
customColoumn2.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%property{CustomColumn2}" ) );
adoNet.BufferSize = 1;
adoNet.CommandType = System.Data.CommandType.Text;
adoNet.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
adoNet.ConnectionString = "data source=" + SQLServer + "; initial catalog=" + SQLDatenBank + ";integrated security=true";
adoNet.CommandText = "INSERT INTO " + SQLTable + " ([Date],[Thread],[Level],[Logger],[Message],[Exception],[CustomColoumn1],[CustomColoumn2]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @customValue1, @customValue2)";
adoNet.AddParameter( logDate );
adoNet.AddParameter( thread );
adoNet.AddParameter( logLevel );
adoNet.AddParameter( logLogger );
adoNet.AddParameter( message );
adoNet.AddParameter( exception );
adoNet.AddParameter( customColoumn1 );
adoNet.AddParameter( customColoumn2 );
adoNet.ActivateOptions();
log4net.LogicalThreadContext.Properties["CustomColumn2"] = customContent2;
i = 1;
}
else
{
log4net.LogicalThreadContext.Properties["CustomColumn2"] = customContent2;
}
}
}
}
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log;
namespace ConsoleApplication1
{
class Program
{
static System.Type className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
static void Main( string[] args )
{
LoggerLib loggerLib = new LoggerLib();
loggerLib.DeclareClass( className );
loggerLib.LoggerSetup(".","test","Log4", "TestColoum");
loggerLib.LogError( "TEXT5", "test4" );
loggerLib.LogError( "TEXT3", "test3" );
}
}
}