當前位置:編程學習大全網 - 源碼下載 - 如何在通用權限管理系統中集成log4net日誌功能

如何在通用權限管理系統中集成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);

}

}

}

  • 上一篇:格力空調e9的故障是什麽,如何排除?
  • 下一篇:彩虹QQ為什麽不好用?
  • copyright 2024編程學習大全網