没有 app.config 的 log4net
log4net without app.config
我正在尝试使用 adoNetappender 登录到 sql 服务器,它与 app.config 一起工作,但我需要它 whiteout 一个 app.config 这是我的 adoNetappender 测试代码。
[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 服务器。
我没有收到任何错误消息,所以我不知道出了什么问题。
谢谢@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
您也可以设置为:ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data"
.
这样你就不会将自己绑定到依赖项的特定版本。
我发现使用特定的 Version=1.0.3300.0 会不断地创建与数据库的新连接。此类连接保留在 SQL 端的连接池中,不会被类似请求重用 - 预期池中的连接将在同一上下文中重用。
我的应用程序会定期关闭,因为连接池已达到其限制并且没有可供应用程序使用的可用连接。
当我删除版本(使用我上面粘贴的代码)时,连接池变得更加稳定,因为连接被重用。
该特定版本背后的原因是什么?我还没有在网上找到任何东西。
我正在尝试使用 adoNetappender 登录到 sql 服务器,它与 app.config 一起工作,但我需要它 whiteout 一个 app.config 这是我的 adoNetappender 测试代码。
[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 服务器。 我没有收到任何错误消息,所以我不知道出了什么问题。
谢谢@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
您也可以设置为:ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data"
.
这样你就不会将自己绑定到依赖项的特定版本。
我发现使用特定的 Version=1.0.3300.0 会不断地创建与数据库的新连接。此类连接保留在 SQL 端的连接池中,不会被类似请求重用 - 预期池中的连接将在同一上下文中重用。 我的应用程序会定期关闭,因为连接池已达到其限制并且没有可供应用程序使用的可用连接。
当我删除版本(使用我上面粘贴的代码)时,连接池变得更加稳定,因为连接被重用。
该特定版本背后的原因是什么?我还没有在网上找到任何东西。