public static IAppender GetSqliteAppender(string dbFilename) { var dbFile = new FileInfo(dbFilename); if (!dbFile.Exists) { CreateLogDb(dbFile); } var appender = new AdoNetAppender { BufferSize = 1, ConnectionType = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite", ConnectionString = SqlLiteDataAccess.LoadConnectionString(), CommandText = "INSERT INTO Board (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" }; //appender.AddParameter(new AdoNetAppenderParameter //{ // ParameterName = "@SerialNumber", // DbType = DbType.String, // Layout = new Layout2RawLayoutAdapter(new PatternLayout("%serialnumber")) //}); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Date", DbType = DbType.DateTime, Layout = new RawTimeStampLayout() }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Level", DbType = DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Logger", DbType = DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Message", DbType = DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) }); appender.ActivateOptions(); return(appender); }
public static IAppender CreateAdoNetAppender(string name, string connectionString, string spocName, IFilter levelFilter) { var adoNetAppender = new AdoNetAppender() { Name = name, BufferSize = 1, ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", ConnectionString = connectionString, CommandType = CommandType.StoredProcedure, CommandText = spocName, UseTransactions = false }; //-- Create parameters adoNetAppender.AddParameter(CreateParameter("@DateCreated", DbType.DateTime, null)); adoNetAppender.AddParameter(CreateParameter("@Application", DbType.String, new PatternLayout(_applicationName), 250)); adoNetAppender.AddParameter(CreateParameter("@Level", DbType.String, new PatternLayout("%p"))); adoNetAppender.AddParameter(CreateParameter("@Class", DbType.String, new PatternLayout("%c"), 250)); adoNetAppender.AddParameter(CreateParameter("@Thread", DbType.Int32, new PatternLayout("%t"))); adoNetAppender.AddParameter(CreateParameter("@Message", DbType.String, new PatternLayout("%m"), 8000)); adoNetAppender.AddParameter(CreateParameter("@Exception", DbType.String, new PatternLayout("%exception"), 8000)); adoNetAppender.AddParameter(CreateParameter("@Operation", DbType.String, new PatternLayout("InsertError"), 150)); adoNetAppender.AddFilter(levelFilter); adoNetAppender.ActivateOptions(); return(adoNetAppender); }
public void NullPropertyProgmaticConfig() { AdoNetAppender appender = new AdoNetAppender(); appender.ConnectionType = typeof(Log4NetConnection).FullName; appender.BufferSize = -1; appender.CommandText = "INSERT INTO Log ([productId]) VALUES (@productId)"; appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@productId", DbType = DbType.String, Size = 50, Layout = new RawPropertyLayout() { Key = "ProductId" } }); appender.ActivateOptions(); ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); BasicConfigurator.Configure(rep, appender); ILog log = LogManager.GetLogger(rep.Name, "NullPropertyProgmaticConfig"); log.Debug("Message"); IDbCommand command = Log4NetCommand.MostRecentInstance; IDbDataParameter param = (IDbDataParameter)command.Parameters["@productId"]; Assert.AreNotEqual(SystemInfo.NullText, param.Value); Assert.AreEqual(DBNull.Value, param.Value); }
public static IAppender GetSqliteAppender(string dbFilename) { var dbFile = new FileInfo(dbFilename); if (!dbFile.Exists) { CreateLogDb(dbFile); } var appender = new AdoNetAppender { ConnectionType = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite", ConnectionString = String.Format("Data Source={0};Version=3;", dbFilename), CommandText = "INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" }; appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Date", DbType = DbType.DateTime, Layout = new RawTimeStampLayout() }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Level", DbType = DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Logger", DbType = DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Message", DbType = DbType.String, Layout = new RawPropertyLayout { Key = "RenderedMessage" } }); appender.ActivateOptions(); return(appender); }
/// <summary> /// CreateAdoNetAppender /// </summary> /// <returns>返回logName</returns> public static string CreateAdoNetAppender() { var hier = LogManager.GetRepository(); if (hier == null) { return(string.Empty); } AdoNetAppender adoAppender = new AdoNetAppender(); adoAppender.Name = string.Format("AdoNetAppender_{0}", DateTime.Now.Ticks); adoAppender.CommandType = System.Data.CommandType.Text; adoAppender.BufferSize = 1; adoAppender.ConnectionType = "MySql.Data.MySqlClient.MySqlConnection, MySql.Data"; adoAppender.ConnectionString = LogDBString; //"Server=192.168.1.30;Database=logdb;Uid=dev;[email protected];Port=3306;"; adoAppender.CommandText = @"INSERT INTO `log`(`Level`,`APPID`,`ServerIP`,`Title`,`Index`,`Message`,`Exception`,`LogDate`) values(@level,@appid,@serverip,@title,@index, @message,@exception, @logdate)"; //adoAppender.CommandText = @"INSERT INTO `log`(`Level`,`Title`,`Index`,`Message`,`Exception`,`LogDate`) // values(@level,@title,@index, @message,@exception, @logdate)"; adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@level", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@appid", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new CustomLayout("%appid")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@serverip", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new CustomLayout("%serverip")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@method", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new CustomLayout("%method")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@title", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new CustomLayout("%title")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@index", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new CustomLayout("%index")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@message", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new CustomLayout("%clientMessage")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logdate", DbType = System.Data.DbType.DateTime, Layout = new RawTimeStampLayout() }); adoAppender.ActivateOptions(); BasicConfigurator.Configure(adoAppender); return(adoAppender.Name); }
public static void NewDatePara(this AdoNetAppender ado, string paraName, string split) { PatternLayout layout = new MyLayout() { ConversionPattern = "%property{".Fmt(paraName).Fmt("}") }; layout.ActivateOptions(); ado.AddParameter(new AdoNetAppenderParameter { ParameterName = "".Fmt(split).Fmt(paraName), DbType = System.Data.DbType.DateTime, Layout = new Layout2RawLayoutAdapter(layout) }); }
public static void LoadADONetAppender(string name, string bufferSize, string connectionType, string connectionString, string CommandText) { Hierarchy hier = LogManager.GetRepository() as Hierarchy; if (hier != null) { AdoNetAppender appender = new AdoNetAppender(); appender.Name = name; appender.CommandType = CommandType.Text; appender.BufferSize = Convert.ToInt32(bufferSize); appender.ConnectionType = connectionType; appender.ConnectionString = connectionString; appender.CommandText = @CommandText; var appId = ConfigurationManager.AppSettings[LogConstants.AppId] ?? LogConstants.DefaultAppId; var localIpAddress = GetLocalIpAddress(); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_AppId, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout(appId)) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_Ip, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout(localIpAddress)) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_LogDate, DbType = DbType.DateTime, Layout = new RawTimeStampLayout() }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_Thread, DbType = DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout(LogConstants.V_Thread)) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_Level, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout(LogConstants.V_Level)) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_Logger, DbType = DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout(LogConstants.V_Logger)) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_Message, DbType = DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new PatternLayout(LogConstants.V_Exception)) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = LogConstants.P_Exception, DbType = DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); appender.ActivateOptions(); BasicConfigurator.Configure(appender); } }
// AdoNetAppender : 로그를 MS SQL Server에 Insert 한다. public static IAppender GetMsSqlAppender(string dataSource, string dbName, string user, string password, int bufferSize = 1) { #region 로그 테이블 스키마 //CREATE TABLE [dbo].[TbLog] ( // [Id] [int] IDENTITY (1, 1) NOT NULL, // [Date] [datetime] NOT NULL, // [Thread] [varchar] (255) NOT NULL, // [Level] [varchar] (50) NOT NULL, // [Logger] [varchar] (255) NOT NULL, // [Message] [varchar] (4000) NOT NULL, // [Exception] [varchar] (2000) NULL, // [User] [varchar] (50) NULL, // [Host] [varchar] (50) NULL //) #endregion var appender = new AdoNetAppender { BufferSize = bufferSize, // 버퍼 사이즈 만큼 로그가 쌓이면 DB에 저장 (로그 발생 시 마다 저장할려면 1) ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089", ConnectionString = $"data source={dataSource};initial catalog={dbName};User ID={user};Password={password};persist security info=True;integrated security=false;", CommandText = "INSERT INTO TbLog([Date], [Thread], [Level], [Logger], [Message], [Exception], [User], [Host]) VALUES(@log_date, @thread, @log_level, @logger, @message, @exception, @user, @host)", }; #region 라미터 설정 appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "log_date", DbType = System.Data.DbType.DateTime, Layout = new RawUtcTimeStampLayout() }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "thread", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%thread")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "log_level", DbType = System.Data.DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "logger", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "message", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "exception", DbType = System.Data.DbType.String, Size = 2000, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "user", DbType = System.Data.DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{log4net:UserName}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "host", DbType = System.Data.DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{log4net:HostName}")) }); #endregion appender.ActivateOptions(); return(appender); }
public void ReallyAdoNetAppenderWithProperty2ByCode() { ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); #region AdoNetAppender & AdoNetAppenderParameter AdoNetAppender adoAppender = new AdoNetAppender(); adoAppender.Name = "AdoNetAppender"; adoAppender.CommandType = CommandType.Text; adoAppender.BufferSize = 1; //被设置为小于或等于1的值,则不会发生缓冲。 adoAppender.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; adoAppender.ConnectionString = "Data Source=192.168.0.214;Initial Catalog=SipscMemberLog;Integrated Security=False;Persist Security Info=False;User ID=sml;Password=sml123;"; adoAppender.CommandText = @"INSERT INTO [Student]([Id],[Name],[Age],[Phone],[Address],[AddDate])VALUES(@Id,@Name,@Age,@Phone,@Address,@AddDate)"; //@Id,@Name,@Age,@Phone,@Address,@AddDate //adoAppender.AddParameter(new AdoNetAppenderParameter() //{ // ParameterName = "@Id", // DbType = DbType.Guid, // Layout = new GuidPropertyLayout() //}); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@Id", DbType = DbType.String, Size = 50, Layout = new RawPropertyLayout() { Key = "Id" } }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@Name", DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{Name}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@Age", DbType = DbType.Int32, Layout = new RawPropertyLayout() { Key = "Age" } }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@Phone", DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{Phone}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@Address", DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{Address}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@AddDate", DbType = DbType.DateTime, //Layout = new RawTimeStampLayout() Layout = new RawPropertyLayout() { Key = "AddDate" } }); #endregion adoAppender.ActivateOptions(); BasicConfigurator.Configure(rep, adoAppender); ILog log = LogManager.GetLogger(rep.Name, "ReallyAdoNetAppenderByCode"); var logEventData = new LoggingEventData() { Level = Level.Info, TimeStampUtc = DateTime.UtcNow, Properties = new PropertiesDictionary() }; logEventData.Properties["Id"] = Guid.NewGuid().ToString(); logEventData.Properties["Name"] = $"name{Guid.NewGuid().ToString().Substring(0, 10)}"; logEventData.Properties["Age"] = new Random().Next(5, 35); logEventData.Properties["Phone"] = "13012345678"; logEventData.Properties["Address"] = "苏州东大街"; logEventData.Properties["AddDate"] = DateTime.UtcNow.ToLocalTime(); log.Logger.Log(new LoggingEvent(logEventData)); Assert.IsTrue(true); }
public void ReallyAdoNetAppenderWithPropertyByCode() { ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); #region AdoNetAppender & AdoNetAppenderParameter AdoNetAppender adoAppender = new AdoNetAppender(); adoAppender.Name = "AdoNetAppender"; adoAppender.CommandType = CommandType.Text; adoAppender.BufferSize = 1; //被设置为小于或等于1的值,则不会发生缓冲。 adoAppender.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; adoAppender.ConnectionString = "Data Source=192.168.0.214;Initial Catalog=SipscMemberLog;Integrated Security=False;Persist Security Info=False;User ID=sml;Password=sml123;"; adoAppender.CommandText = @"INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Prop1],[Prop2],[Prop3]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @prop1, @prop2, @prop3)"; //日志记录时间:RawTimeStampLayout为默认的时间输出格式。 adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@log_date", DbType = DbType.DateTime, Layout = new RawTimeStampLayout() }); //线程号 adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@thread", Size = 255, //长度不可以省略,否则不会输出。 Layout = new Layout2RawLayoutAdapter(new PatternLayout("%thread")) }); //日志等级 adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@log_level", Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); //日志记录类名称 adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@logger", DbType = DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@message", DbType = DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@exception", DbType = DbType.String, Size = 2000, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%exception")) }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@prop1", DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{prop1}")) }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@prop2", DbType = DbType.Int32, //Size = 50, Layout = new RawPropertyLayout() { Key = "prop2" } }); adoAppender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@prop3", DbType = DbType.Guid, Layout = new GuidPropertyLayout() }); #endregion adoAppender.ActivateOptions(); BasicConfigurator.Configure(rep, adoAppender); ILog log = LogManager.GetLogger(rep.Name, "ReallyAdoNetAppenderByCode"); //log.Info($"Message {Guid.NewGuid().ToString()}"); //log.Error($"出错啦 {Guid.NewGuid().ToString()}", new Exception("模拟异常")); var logEventData = new LoggingEventData() { TimeStampUtc = DateTime.UtcNow, Level = Level.Info, Message = $"test-{Guid.NewGuid()}", Properties = new PropertiesDictionary() }; logEventData.Properties["prop1"] = "prop111"; logEventData.Properties["prop2"] = 111; log.Logger.Log(new LoggingEvent(logEventData)); Assert.IsTrue(true); }
/// <summary> /// 设置配置 /// </summary> public void SetConfig() { //不是第一次设置 if (rep != null) { rep.ResetConfiguration(); } else//初次创建 { //创建Repository try { rep = LogManager.GetRepository(logName); //log = LogManager.GetLogger(rep.Name, logName); } catch { rep = LogManager.CreateRepository(logName); log4net.Config.BasicConfigurator.Configure(rep, fileApp); log = LogManager.GetLogger(logName, logName); } } //重设文件输出 RollingFileAppender fileAppender = new RollingFileAppender(); fileAppender.Layout = new PatternLayout(strLayoutPattern); fileAppender.RollingStyle = (RollingFileAppender.RollingMode)rollingMode; if (rollingMode == RollingType.Date) { fileAppender.StaticLogFileName = false; fileAppender.DatePattern = @"\\\\yyyy-MM-dd\\\\yyyyMMdd'.log'"; fileAppender.File = filePath; if (fileAppender.File != null && fileAppender.File.EndsWith(".log")) { fileAppender.File = fileAppender.File.Remove(fileAppender.File.IndexOf(".log")); } } else { fileAppender.File = filePath; } fileAppender.LockingModel = new RollingFileAppender.MinimalLock(); fileAppender.AppendToFile = true; fileAppender.Encoding = Encoding.GetEncoding("GB2312"); //fileAppender.CountDirection = 1; fileAppender.MaxFileSize = maxFileSize; fileAppender.MaxSizeRollBackups = this.MaxSizeRollBackups; fileAppender.ActivateOptions(); fileApp = fileAppender; if ((logType & LogType.File) == LogType.File) { //创建Log文件输出方式 log4net.Config.BasicConfigurator.Configure(rep, fileApp); } //控制台输出 if ((logType & LogType.Console) == LogType.Console) { PatternLayout layout = new PatternLayout(strLayoutPattern); ConsoleAppender appender = new ConsoleAppender(); appender.Layout = layout; log4net.Config.BasicConfigurator.Configure(rep, appender); } //数据库输出 if ((logType & LogType.DataBase) == LogType.DataBase) { AdoNetAppender appender = new AdoNetAppender(); appender.ConnectionString = DBConnectionString; appender.CommandText = DBInsertSql; if (DBParameter != null) { for (int i = 0; i < DBParameter.Length; i++) { appender.AddParameter(DBParameter[i]); } } log4net.Config.BasicConfigurator.Configure(rep, appender); } //Debug输出 if ((logType & LogType.Debug) == LogType.Debug) { DebugAppender appender = new DebugAppender(); appender.Layout = new PatternLayout(strLayoutPattern); log4net.Config.BasicConfigurator.Configure(rep, appender); } log = LogManager.GetLogger(rep.Name, logName); }
/// <summary> /// 如果不想自定义log4net 配置文件,该方法提供一个默认的logger,该logger直接写数据库; /// </summary> private static void SetDefaultLogger() { AdoNetAppender appender = new AdoNetAppender(); appender.BufferSize = 0; appender.ConnectionType = @"System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; appender.ConnectionString = @"data source=(LocalDb)\MSSQLLocalDB;initial catalog=log;integrated security=true;persist security info=True;"; appender.CommandText = @"INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [ComputerName]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @ComputerName)"; appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@log_date", DbType = System.Data.DbType.DateTime, Size = 100, //Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}")) as IRawLayout, Layout = new RawTimeStampLayout(), }); appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@thread", DbType = System.Data.DbType.String, Size = 255, //Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%thread")) as IRawLayout, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%thread")), }); appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@log_level", DbType = System.Data.DbType.String, Size = 50, Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%level")) as IRawLayout }); appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@logger", DbType = System.Data.DbType.String, Size = 255, Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%logger")) as IRawLayout }); appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@message", DbType = System.Data.DbType.String, Size = 4000, Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%message")) as IRawLayout }); appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 2000, Layout = new RawLayoutConverter().ConvertFrom(new ExceptionLayout()) as IRawLayout }); appender.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@ComputerName", DbType = System.Data.DbType.String, Size = 255, Layout = new RawLayoutConverter().ConvertFrom(new PatternLayout("%property{log4net:HostName}")) as IRawLayout, }); appender.ActivateOptions(); BasicConfigurator.Configure(appender); // 如果没有配置logger name,默认使用DefaultLogger这个名字; logger = LogManager.GetLogger(ConfigHelper.GetString("loggerName") ?? "DefaultLogger"); }
private static IAppender CreateAdoNetAppender(string connectionString) { var appender = new AdoNetAppender { Name = "AdoNetAppender", ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089", ConnectionString = connectionString, BufferSize = 1, CommandText = @"INSERT INTO[LogEntries]( [LogDate], [Logger], [LogLevel], [Thread], [EntityFormalNamePlural], [EntityKeyValue], [UserName], [Message], [Exception]) VALUES(@log_date, @logger, @log_level, @thread, @entityFormalNamePlural, @entityKeyValue, @userName, @message, @exception)", CommandType = System.Data.CommandType.Text }; appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_date", DbType = System.Data.DbType.DateTime, Layout = new RawTimeStampLayout() }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logger", DbType = System.Data.DbType.String, Size = 256, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_level", DbType = System.Data.DbType.String, Size = 5, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@thread", DbType = System.Data.DbType.String, Size = 10, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%thread")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@entityFormalNamePlural", DbType = System.Data.DbType.String, Size = 30, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{EntityFormalNamePlural}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "entityKeyValue", DbType = System.Data.DbType.Int32, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{EntityKeyValue}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@userName", DbType = System.Data.DbType.String, Size = 256, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%property{UserName}|%aspnet-request{AUTH_USER}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@message", DbType = System.Data.DbType.String, Size = 256, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 10000, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); appender.ActivateOptions(); return(appender); }
private static IAppender GetAdoAppender() { RawLayoutConverter layoutConverter = new RawLayoutConverter(); var databaseAppender = new AdoNetAppender { BufferSize = 1, Name = "AdoAppender", //ConnectionString = "server=localhost;database=log; user=nelson;password=nightmare;port=3306", ConnectionString = @"Data Source=CLINSVNB03\SQLEXPRESS2012;Initial Catalog=CLINERP;User ID=sa;Password=P@ssw0rd;", CommandText = "INSERT INTO auditoria_ws (MENSAJE,URL,IP,PARAMETROS, PARAMETROS_GET, CLIENTE, METODO , FECHA, EXCEPCION) " + "VALUES (@message, @url, @ip, @params, @query, @cliente,@metodo, GETDATE(), @exception)", CommandType = System.Data.CommandType.Text, ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", //ConnectionType = "MySql.Data.MySqlClient.MySqlConnection, MySql.Data" }; databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logdate", DbType = System.Data.DbType.DateTime, Layout = new RawTimeStampLayout() }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@message", DbType = System.Data.DbType.String, Size = 4000, Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout("%m")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 2000, Layout = (IRawLayout)layoutConverter.ConvertFrom(new ExceptionLayout()) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@level", DbType = System.Data.DbType.String, Size = 50, Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout("%-5p")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logger", DbType = System.Data.DbType.String, Size = 255, Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout("%c")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@thread", DbType = System.Data.DbType.String, Size = 255, Layout = (IRawLayout)layoutConverter.ConvertFrom(new PatternLayout("%t")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@url", DbType = System.Data.DbType.String, Size = 8000, Layout = (IRawLayout)layoutConverter.ConvertFrom(new UrlPatternLayout("%url%")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@query", DbType = System.Data.DbType.String, Size = 8000, Layout = (IRawLayout)layoutConverter.ConvertFrom(new UrlPatternLayout("%query%")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@ip", DbType = System.Data.DbType.String, Size = 50, Layout = (IRawLayout)layoutConverter.ConvertFrom(new UrlPatternLayout("%ip%")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@cliente", DbType = System.Data.DbType.String, Size = 255, Layout = (IRawLayout)layoutConverter.ConvertFrom(new UrlPatternLayout("%cli%")) }); //metodo databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@metodo", DbType = System.Data.DbType.String, Size = 50, Layout = (IRawLayout)layoutConverter.ConvertFrom(new UrlPatternLayout("%method%")) }); databaseAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@params", DbType = System.Data.DbType.String, Layout = (IRawLayout)layoutConverter.ConvertFrom(new UrlPatternLayout("%params%")) }); databaseAppender.ActivateOptions(); return(databaseAppender); }
private static IAppender CreatAdoNetAppender(string connectionString) { var appender = new AdoNetAppender { Name = "AdoNetAppender", ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089", ConnectionString = connectionString, BufferSize = 1, CommandText = "INSERT INTO Log ([Date],[Username],[Thread],[Level],[Logger],[Message],[Exception]) " + "VALUES (@log_date, @log_username, @thread, @log_level, @logger, @message, @exception)", CommandType = System.Data.CommandType.Text }; appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_date", DbType = System.Data.DbType.DateTime, Layout = new RawTimeStampLayout() }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_username", DbType = System.Data.DbType.String, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%aspnet-request{AUTH_USER}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@thread", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%thread")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_level", DbType = System.Data.DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logger", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@message", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 2000, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); appender.ActivateOptions(); return(appender); }
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; } }
protected void Setup() { if (!_isSetup) { // add database appender for warnings var appender = new AdoNetAppender { BufferSize = 1, ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", CommandType = System.Data.CommandType.Text, ConnectionString = _connectionString, CommandText = "INSERT INTO RIFF.[SystemLog] ( Timestamp, Hostname, Level, Source, Message, Exception, Thread, AppDomain ) VALUES ( @timestamp, @hostname, @level, @source, @message, @exception, @thread, @appdomain )", Threshold = Level.Warn }; var rlc = new RawLayoutConverter(); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@timestamp", DbType = System.Data.DbType.String, Size = 50, Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%date{yyyy-MM-ddTHH:mm:ss.fffzzz}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@hostname", DbType = System.Data.DbType.String, Size = 100, Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%property{log4net:HostName}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@level", DbType = System.Data.DbType.String, Size = 50, Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%level")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@source", DbType = System.Data.DbType.String, Size = 255, Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%property{NDC}")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@message", DbType = System.Data.DbType.String, Size = 4000, Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%message")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 2000, Layout = (IRawLayout)rlc.ConvertFrom(new ExceptionLayout()) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@thread", DbType = System.Data.DbType.String, Size = 50, Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%thread")) }); appender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@appdomain", DbType = System.Data.DbType.String, Size = 50, Layout = (IRawLayout)rlc.ConvertFrom(new PatternLayout("%appdomain")) }); appender.ActivateOptions(); var hierarchy = (Hierarchy)LogManager.GetRepository(); hierarchy.Root.AddAppender(appender); hierarchy.Configured = true; _isSetup = true; } _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); }
static Logger() { NameValueCollection appSettings = ConfigurationManager.AppSettings; string connectString = appSettings["LogConnectString"]; string currentPath = AppDomain.CurrentDomain.BaseDirectory; string txtLogPath = string.Empty; string iisBinPath = AppDomain.CurrentDomain.RelativeSearchPath; if (!string.IsNullOrEmpty(iisBinPath)) { txtLogPath = Path.Combine(iisBinPath, @"Logs\ErrorLog.txt"); } else { txtLogPath = Path.Combine(currentPath, @"Logs\ErrorLog.txt"); } Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); TraceAppender tracer = new TraceAppender(); PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = LOG_PATTERN; patternLayout.ActivateOptions(); tracer.Layout = patternLayout; tracer.ActivateOptions(); hierarchy.Root.AddAppender(tracer); RollingFileAppender roller = new RollingFileAppender(); roller.Layout = patternLayout; roller.AppendToFile = true; roller.RollingStyle = RollingFileAppender.RollingMode.Size; roller.MaxSizeRollBackups = 4; roller.MaximumFileSize = "5MB";//"100KB"; roller.Encoding = Encoding.UTF8; roller.StaticLogFileName = true; roller.File = txtLogPath;//"dnservices.txt"; roller.ActivateOptions(); hierarchy.Root.AddAppender(roller); /////////////////////////ADO.NET AdoNetAppender adoAppender = new AdoNetAppender(); adoAppender.Name = "AdoNetAppender"; adoAppender.CommandType = CommandType.Text; adoAppender.BufferSize = 1; adoAppender.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; adoAppender.ConnectionString = connectString; adoAppender.CommandText = @"INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"; adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_date", DbType = System.Data.DbType.DateTime, Layout = new log4net.Layout.RawTimeStampLayout() }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@thread", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%thread")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_level", DbType = System.Data.DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logger", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@message", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message")) }); adoAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); adoAppender.ActivateOptions(); hierarchy.Root.AddAppender(adoAppender); hierarchy.Root.Level = Level.All; hierarchy.Configured = true; }
private static IAppender GetAdoAppender(IConfigManagerHelper config) { var connectionString = config.GetConnectionString("OneHydraLog"); if (string.IsNullOrEmpty(connectionString)) { throw new Exception("A connection string to the log database is required. Please add the connection string by the name OneHydraLog to the config and make sure the connection string points to a database with the expected OneSearchLog table."); } var adoAppender = new AdoNetAppender { Name = "ADONetAppender", BufferSize = 1, ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", ConnectionString = connectionString, CommandText = "INSERT INTO [OneSearchLog] ([Date],[Thread],[Level],[Logger],[Message],[Exception], [MachineName]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @machineName)", CommandType = CommandType.Text }; //<conversionPattern value="%property{log4net:HostName}" /> var parameter = new AdoNetAppenderParameter { ParameterName = "@log_date", DbType = DbType.DateTime }; var patternLayout = new PatternLayout { ConversionPattern = "%date{MM/dd/yyyy HH:mm:ss}" }; patternLayout.ActivateOptions(); parameter.Layout = new Layout2RawLayoutAdapter(patternLayout); adoAppender.AddParameter(parameter); parameter = new AdoNetAppenderParameter { ParameterName = "@thread", DbType = DbType.String, Size = 255 }; patternLayout = new PatternLayout { ConversionPattern = "%thread" }; patternLayout.ActivateOptions(); parameter.Layout = new Layout2RawLayoutAdapter(patternLayout); adoAppender.AddParameter(parameter); parameter = new AdoNetAppenderParameter { ParameterName = "@log_level", DbType = DbType.String, Size = 50 }; patternLayout = new PatternLayout { ConversionPattern = "%level" }; patternLayout.ActivateOptions(); parameter.Layout = new Layout2RawLayoutAdapter(patternLayout); adoAppender.AddParameter(parameter); parameter = new AdoNetAppenderParameter { ParameterName = "@logger", DbType = DbType.String, Size = 255 }; patternLayout = new PatternLayout { ConversionPattern = "%logger" }; patternLayout.ActivateOptions(); parameter.Layout = new Layout2RawLayoutAdapter(patternLayout); adoAppender.AddParameter(parameter); parameter = new AdoNetAppenderParameter { ParameterName = "@message", DbType = DbType.String, Size = 200000000 }; patternLayout = new PatternLayout { ConversionPattern = "%message" }; patternLayout.ActivateOptions(); parameter.Layout = new Layout2RawLayoutAdapter(patternLayout); adoAppender.AddParameter(parameter); parameter = new AdoNetAppenderParameter { ParameterName = "@machineName", DbType = DbType.String, Size = 255 }; patternLayout = new PatternLayout { ConversionPattern = "%property{machineName}" }; patternLayout.ActivateOptions(); parameter.Layout = new Layout2RawLayoutAdapter(patternLayout); adoAppender.AddParameter(parameter); parameter = new AdoNetAppenderParameter { ParameterName = "@exception", DbType = DbType.String, Size = 200000000 }; patternLayout = new PatternLayout { ConversionPattern = "%property{fullException}" }; patternLayout.ActivateOptions(); parameter.Layout = new Layout2RawLayoutAdapter(patternLayout); //var exceptionLayout = new ExceptionLayout(){IgnoresException = true}; //exceptionLayout.ActivateOptions(); //parameter.Layout = new Layout2RawLayoutAdapter(exceptionLayout); adoAppender.AddParameter(parameter); adoAppender.ActivateOptions(); return(adoAppender); }
static void CreateAdoNetLogger(string repositoryName) { ILoggerRepository rep = LogManager.GetRepository(repositoryName); string sqlServerConnStr = ConfigurationManager.ConnectionStrings["DbContext"].ToString(); AdoNetAppender adoNetAppender = new AdoNetAppender(); adoNetAppender.BufferSize = -1; adoNetAppender.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"; adoNetAppender.ConnectionString = sqlServerConnStr; adoNetAppender.CommandText = @"INSERT INTO SysLog ([Date],[Level],[Logger],[Message],[Exception],[MesID],[InterFaceNo],[Mac],[IP],[InterFaceName],[ObjectName],[RequestUrl],Data,Status,Response,RequestSystem) VALUES (@log_date, @log_level, @logger, @Message, @exception,@MesID,@InterFaceNo,@Mac,@IP,@InterFaceName,@ObjectName,@RequestUrl,@Data,@Status,@Response,@RequestSystem)"; adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_date", DbType = System.Data.DbType.DateTime, Layout = new log4net.Layout.RawTimeStampLayout() }); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@log_level", DbType = System.Data.DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level")) }); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@logger", DbType = System.Data.DbType.String, Size = 255, Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger")) }); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@exception", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(new ExceptionLayout()) }); PatternLayout layout = new MyLayout() { ConversionPattern = "%property{MesID}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@MesID", DbType = System.Data.DbType.String, Size = 200, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{InterFaceNo}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@InterFaceNo", DbType = System.Data.DbType.String, Size = 200, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Mac}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Mac", DbType = System.Data.DbType.String, Size = 200, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{IP}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@IP", DbType = System.Data.DbType.String, Size = 200, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{InterFaceName}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@InterFaceName", DbType = System.Data.DbType.String, Size = 200, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{ObjectName}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@ObjectName", DbType = System.Data.DbType.String, Size = 200, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{RequestUrl}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@RequestUrl", DbType = System.Data.DbType.String, Size = 500, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Message}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Message", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Data}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Data", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Status}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Status", DbType = System.Data.DbType.String, Size = 80, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{Response}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@Response", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); layout = new MyLayout() { ConversionPattern = "%property{RequestSystem}" }; layout.ActivateOptions(); adoNetAppender.AddParameter(new AdoNetAppenderParameter { ParameterName = "@RequestSystem", DbType = System.Data.DbType.String, Size = 4000, Layout = new Layout2RawLayoutAdapter(layout) }); adoNetAppender.ActivateOptions(); BasicConfigurator.Configure(rep, adoNetAppender); }