log4net 代码配置

public class Log4netUtility {

    public Log4netUtility(){}

    static Log4netUtility(){

        log4net.Repository.Hierarchy.Hierarchy hier = log4net.LogManager.GetRepository() as log4net.Repository.Hierarchy.Hierarchy;

         if (hier != null){

            AdoNetAppender adoAppender = new log4net.Appender.AdoNetAppender();

                adoAppender.Name = "AdoNetAppender";

                adoAppender.CommandType = CommandType.Text;

                adoAppender.BufferSize = 1;//缓存提交

                adoAppender.ConnectionType = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite";

                adoAppender.ConnectionString = @"Data Source=" + SQLiteUtility.SQLITE_FILE_FULLNAME;//data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]

                adoAppender.CommandText = @"INSERT INTO SysLog (Date, Level, Logger, Message,DataStatus,InputUser,InputDatetime) VALUES (@Date, @Level, @Logger, @Message,@DataStatus,@InputUser,@InputDatetime)";

                adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Date", DbType = System.Data.DbType.DateTime, Layout = new log4net.Layout.RawTimeStampLayout() });

                adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Level", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) });

                adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Logger", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) });

                adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Message", DbType = System.Data.DbType.String,Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) });

                adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@DataStatus", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{DataStatus}")) });

                adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@InputUser", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{InputUser}")) });

                adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@InputDatetime", DbType = System.Data.DbType.String, Layout = new log4net.Layout.RawTimeStampLayout() });

                adoAppender.ActivateOptions();

                BasicConfigurator.Configure(adoAppender);

        }

}

public static void WriteErrorLog(Type t, Exception ex)

        {         

            log4net.ILog log = log4net.LogManager.GetLogger(t);

            log4net.LogicalThreadContext.Properties["DataStatus"] = "0";

            if (ECDSConstants.CURRENT_USER != null)

            {

                log4net.LogicalThreadContext.Properties["InputUser"] = ECDSConstants.CURRENT_USER.UserName;

            }

            else

            {

                log4net.LogicalThreadContext.Properties["InputUser"] = "";

            }

            log.Error(ex.ToString(), ex);

        }

}

你可能感兴趣的:(log4net 代码配置)