体育资讯网

您现在的位置是:首页 > 分类10 > 正文

分类10

log4net源码(log4net appender)

hacker2022-06-10 16:40:23分类1054
本文目录一览:1、如何动态修改log4net日志文件路径

本文目录一览:

如何动态修改log4net日志文件路径

1.配置文档

appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender"

!--配置文件路径log4net源码;这里修改源码log4net源码,在调用LogManager.GetLogger(name)时,name结构是Client_LogType_logger--

file value="logs/%Client/%LogType/" /

!--是否追加到文件--

appendToFile value="true" /

!--最大变换数量,-1为不限制--

MaxSizeRollBackups value="-1" /

!--文件大小--

MaximumFileSize value="1MB"/

encoding value="utf-8" /

!--文件以那种方式变换文件名log4net源码:data日期/Size大小/Composite同时按照日期和大小--

rollingStyle value="Composite" /

!--文件名格式--

datePattern value=""log_"yyyyMMdd

如何在通用权限管理系统中集成log4net日志功能

首先在官网下载最新源码,目前的源码可用VS2010打开。

源码中已经实现了可以日志输出到MSSQL的功能,但我的项目目前使用的都是Oracle数据库,源码中是没有实现的,需要自己实现一下:

public class OracleAppender : BufferingAppenderSkeleton

{

// Fields

private static readonly Type declaringType = typeof(AdoNetAppender);

private string m_commandText;

private CommandType m_commandType = CommandType.Text;

private string m_connectionString;

private string m_connectionType;

private OracleCommand m_dbCommand;

private OracleConnection m_dbConnection;

protected ArrayList m_parameters = new ArrayList();

private bool m_reconnectOnError = false;

private SecurityContext m_securityContext;

protected bool m_usePreparedCommand;

private bool m_useTransactions = true;

// Methods

public override void ActivateOptions()

{

base.ActivateOptions();

this.m_usePreparedCommand = (this.m_commandText != null) (this.m_commandText.Length 0);

if (this.m_securityContext == null)

{

this.m_securityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this);

}

this.InitializeDatabaseConnection();

this.InitializeDatabaseCommand();

}

public void AddParameter(OracleAppenderParameter parameter)

{

this.m_parameters.Add(parameter);

}

protected virtual string GetLogStatement(LoggingEvent logEvent)

{

if (this.Layout == null)

{

this.ErrorHandler.Error("ADOAppender: No Layout specified.");

return "";

}

StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);

this.Layout.Format(writer, logEvent);

return writer.ToString();

}

private void InitializeDatabaseCommand()

{

if ((this.m_dbConnection != null) this.m_usePreparedCommand)

{

try

{

this.m_dbCommand = this.m_dbConnection.CreateCommand();

this.m_dbCommand.CommandText = this.m_commandText;

this.m_dbCommand.CommandType = this.m_commandType;

}

catch (Exception exception)

{

this.ErrorHandler.Error("Could not create database command [" + this.m_commandText + "]", exception);

if (this.m_dbCommand != null)

{

try

{

this.m_dbCommand.Dispose();

}

catch

{

}

this.m_dbCommand = null;

}

}

if (this.m_dbCommand != null)

{

try

{

foreach (OracleAppenderParameter parameter in this.m_parameters)

{

try

{

parameter.Prepare(this.m_dbCommand);

}

catch (Exception exception2)

{

this.ErrorHandler.Error("Could not add database command parameter [" + parameter.ParameterName + "]", exception2);

throw;

}

}

}

catch

{

try

{

this.m_dbCommand.Dispose();

}

catch

{

}

this.m_dbCommand = null;

}

}

if (this.m_dbCommand != null)

{

try

{

this.m_dbCommand.Prepare();

}

catch (Exception exception3)

{

this.ErrorHandler.Error("Could not prepare database command [" + this.m_commandText + "]", exception3);

try

{

this.m_dbCommand.Dispose();

}

catch

{

}

this.m_dbCommand = null;

}

}

}

}

private void InitializeDatabaseConnection()

{

try

{

this.m_dbConnection = new OracleConnection();

this.m_dbConnection.ConnectionString = this.m_connectionString;

using (this.SecurityContext.Impersonate(this))

{

this.m_dbConnection.Open();

}

}

catch (Exception exception)

{

this.ErrorHandler.Error("Could not open database connection [" + this.m_connectionString + "]", exception);

this.m_dbConnection = null;

}

}

protected override void OnClose()

{

base.OnClose();

if (this.m_dbCommand != null)

{

this.m_dbCommand.Dispose();

this.m_dbCommand = null;

}

if (this.m_dbConnection != null)

{

this.m_dbConnection.Close();

this.m_dbConnection = null;

}

}

protected virtual Type ResolveConnectionType()

{

Type type;

try

{

type = SystemInfo.GetTypeFromString(this.m_connectionType, true, false);

}

catch (Exception exception)

{

this.ErrorHandler.Error("Failed to load connection type [" + this.m_connectionType + "]", exception);

throw;

}

return type;

}

protected override void SendBuffer(LoggingEvent[] events)

{

if (this.m_reconnectOnError ((this.m_dbConnection == null) || (this.m_dbConnection.State != ConnectionState.Open)))

{

LogLog.Debug(declaringType, "OracleAppender: Attempting to reconnect to database. Current Connection State: " + ((this.m_dbConnection == null) ? "null" : this.m_dbConnection.State.ToString()));

this.InitializeDatabaseConnection();

this.InitializeDatabaseCommand();

}

if ((this.m_dbConnection != null) (this.m_dbConnection.State == ConnectionState.Open))

{

if (this.m_useTransactions)

{

OracleTransaction dbTran = null;

try

{

dbTran = this.m_dbConnection.BeginTransaction();

this.SendBuffer(dbTran, events);

dbTran.Commit();

}

catch (Exception exception)

{

if (dbTran != null)

{

try

{

dbTran.Rollback();

}

catch (Exception)

{

}

}

this.ErrorHandler.Error("Exception while writing to database", exception);

}

}

else

{

this.SendBuffer(null, events);

}

}

}

如何使用log4net

下载Log4net源码和选择Log4net.config的Log记录配置文件(有的因为在web.config中统一记录)

下载源码后,使用VS工具打开,编译一下项目即可获得log4net.dll文件

如果找不到路径,可以点击vs项目的属性,查看输入路径在哪里,一般为解压根目录下的build目录,当然你可以自己修改

在C#项目中添加Log4net.dll引用,找到项目选择引用,选择好Log4net.dll文件,添加完成后,编译下,在项目的bin文件夹下即可看到Log4net.dll文件

在项目的程序集信息描述文件中,设置Log4net的可记录属性

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

在根目录下添加Log4net的配置文件。可以是App.config、web.config、Log4net.config均可

程序中引用Log4net,记录Log

查看Log文件

Log4net能否写入MySql

我的winform程序也不行,不知道什么地方出了问题。楼主有解决办法麻烦告诉下。 查了下源码,我的似乎是找不到MySql.Data

//////////////////////

我的解决了,似乎直接添加MySql.Data的引用并不起作用,还需要把MySql.Data.dll考到bin目录下,这样它才能找到。

/////////////////////

我的当时也是插入数据库什么的没有问题,你可以用log4net源码自己编译引用,然后就可以进去单步调试了,看什么地方出了问题,我的是数据库连接没有建立成功,但又没有抛出异常。再不行,你就留个邮箱吧。

发表评论

评论列表

  • 余安清淮(2022-06-11 04:14:53)回复取消回复

    public void AddParameter(OracleAppenderParameter parameter) { this.m_parameters.Add(parameter); }

  • 冢渊礼忱(2022-06-11 03:36:37)回复取消回复

    try { dbTran = this.m_dbConnection.BeginTransaction(); this