自己写的 Log日志记录类,支持文件和数据库,自动建立Log表格,刚学设计模式,大家别见笑。
文件:ILog.cs代码
using System;
namespace LZ2007.Function.Log
{
/// <summary>
/// 日志通用接口
/// </summary>
public interface ILog
{
void Info(string message, int level);
void Info(string message);
void Warn(string message, int leave);
void Warn(string message);
void Debug(string message, int leave);
void Debug(string message);
void Error(string message, Exception e, int leave);
void Error(string message, Exception e);
void Fatal(string message, Exception e, int leave);
void Fatal(string message, Exception e);
void Close();
}
}
namespace LZ2007.Function.Log
{
/// <summary>
/// 日志通用接口
/// </summary>
public interface ILog
{
void Info(string message, int level);
void Info(string message);
void Warn(string message, int leave);
void Warn(string message);
void Debug(string message, int leave);
void Debug(string message);
void Error(string message, Exception e, int leave);
void Error(string message, Exception e);
void Fatal(string message, Exception e, int leave);
void Fatal(string message, Exception e);
void Close();
}
}
文件LogManage.cs代码
using System;
using System.Data;
using System.Configuration;
namespace LZ2007.Function.Log
{
/// <summary>
/// 日志工厂类
/// </summary>
public static class LogFactory
{
public static ILog GetLog(Type objType)
{
int _LogType = Convert.ToInt32(ConfigurationManager.AppSettings["eLogType"]);
ILog log = null;
if (_LogType == 1)
{
log = new DataBaseLog(objType);
}
else if(_LogType==0)
{
log = new FileLog(objType);
}
return log;
}
}
}
using System.Data;
using System.Configuration;
namespace LZ2007.Function.Log
{
/// <summary>
/// 日志工厂类
/// </summary>
public static class LogFactory
{
public static ILog GetLog(Type objType)
{
int _LogType = Convert.ToInt32(ConfigurationManager.AppSettings["eLogType"]);
ILog log = null;
if (_LogType == 1)
{
log = new DataBaseLog(objType);
}
else if(_LogType==0)
{
log = new FileLog(objType);
}
return log;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
namespace LZ2007.Function.Log
{
/// <summary>
/// 数据库日志类.
/// </summary>
public class DataBaseLog : ILog
{
私有变量申明
public DataBaseLog(Type objType)
{
_isDebug = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogDebug"]);
_isInfo = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogInfo"]);
_isError = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogError"]);
_isWarn = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogWarn"]);
_LogObjectSource = objType.FullName;
//_LogType = Convert.ToInt32(ConfigurationManager.AppSettings["eLogType"]);
_LogConnStr = ConfigurationManager.AppSettings["eLogConnStr"];
_sqlConn = new SqlConnection(_LogConnStr);
Init();
}
private void Init()
{
//检查是否有该表
string strTest = "select count(name)as a1 from sysobjects where id = object_id(N’[LOGSYSTEM]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1";
//建立表
string strSQL = "CREATE TABLE [dbo].[LOGSYSTEM] (" +
"[lId] [int] IDENTITY (1, 1) NOT NULL ," +
"[lMessage] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lLevel] [int] NULL ," +
"[lSource] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lException] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lType] [int] NULL ," +
"[lDate] [datetime] NULL ," +
"[lADDIT1] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lADDIT2] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lADDIT3] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL " +
") ON [PRIMARY]";
_sqlConn.Open();
SqlCommand sqlcomm = new SqlCommand(strTest, _sqlConn);
int i = (int)sqlcomm.ExecuteScalar(); ;
if (i == 0)
{
sqlcomm = new SqlCommand(strSQL, _sqlConn);
sqlcomm.ExecuteNonQuery();
}
sqlcomm.Dispose();
}
private int insertLog(string message, int level, string source, string exception, int type)
{
string strSQL = "INSERT INTO LOGSYSTEM(lMessage,lLevel,lSource,lException,lType,lDate) " +
"VALUES (@lMessage,@lLevel,@lSource,@lException,@lType,@lDate)";
SqlCommand sqlcomm = new SqlCommand(strSQL, _sqlConn);
if (_sqlConn.State == ConnectionState.Closed)
{
_sqlConn.Open();
}
sqlcomm.Parameters.Add("@lMessage", SqlDbType.NVarChar, 1000);
sqlcomm.Parameters["@lMessage"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lMessage"].Value = message;
sqlcomm.Parameters.Add("@lLevel", SqlDbType.Int);
sqlcomm.Parameters["@lLevel"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lLevel"].Value = level;
sqlcomm.Parameters.Add("@lSource", SqlDbType.NVarChar, 100);
sqlcomm.Parameters["@lSource"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lSource"].Value = source;
sqlcomm.Parameters.Add("@lException", SqlDbType.NVarChar, 100);
sqlcomm.Parameters["@lException"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lException"].Value = exception;
sqlcomm.Parameters.Add("@lType", SqlDbType.Int);
sqlcomm.Parameters["@lType"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lType"].Value = type;
sqlcomm.Parameters.Add("@lDate", SqlDbType.DateTime);
sqlcomm.Parameters["@lDate"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lDate"].Value = DateTime.Now;
return sqlcomm.ExecuteNonQuery();
}
public void Close()
{
this.Info("Log End", 5);
_sqlConn.Close();
}
信息[Info]
警告[Warn]
调试[Debug]
错误[Error]
致命错误[Fatal]
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
namespace LZ2007.Function.Log
{
/// <summary>
/// 数据库日志类.
/// </summary>
public class DataBaseLog : ILog
{
私有变量申明
public DataBaseLog(Type objType)
{
_isDebug = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogDebug"]);
_isInfo = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogInfo"]);
_isError = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogError"]);
_isWarn = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogWarn"]);
_LogObjectSource = objType.FullName;
//_LogType = Convert.ToInt32(ConfigurationManager.AppSettings["eLogType"]);
_LogConnStr = ConfigurationManager.AppSettings["eLogConnStr"];
_sqlConn = new SqlConnection(_LogConnStr);
Init();
}
private void Init()
{
//检查是否有该表
string strTest = "select count(name)as a1 from sysobjects where id = object_id(N’[LOGSYSTEM]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1";
//建立表
string strSQL = "CREATE TABLE [dbo].[LOGSYSTEM] (" +
"[lId] [int] IDENTITY (1, 1) NOT NULL ," +
"[lMessage] [nvarchar] (1000) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lLevel] [int] NULL ," +
"[lSource] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lException] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lType] [int] NULL ," +
"[lDate] [datetime] NULL ," +
"[lADDIT1] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lADDIT2] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[lADDIT3] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL " +
") ON [PRIMARY]";
_sqlConn.Open();
SqlCommand sqlcomm = new SqlCommand(strTest, _sqlConn);
int i = (int)sqlcomm.ExecuteScalar(); ;
if (i == 0)
{
sqlcomm = new SqlCommand(strSQL, _sqlConn);
sqlcomm.ExecuteNonQuery();
}
sqlcomm.Dispose();
}
private int insertLog(string message, int level, string source, string exception, int type)
{
string strSQL = "INSERT INTO LOGSYSTEM(lMessage,lLevel,lSource,lException,lType,lDate) " +
"VALUES (@lMessage,@lLevel,@lSource,@lException,@lType,@lDate)";
SqlCommand sqlcomm = new SqlCommand(strSQL, _sqlConn);
if (_sqlConn.State == ConnectionState.Closed)
{
_sqlConn.Open();
}
sqlcomm.Parameters.Add("@lMessage", SqlDbType.NVarChar, 1000);
sqlcomm.Parameters["@lMessage"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lMessage"].Value = message;
sqlcomm.Parameters.Add("@lLevel", SqlDbType.Int);
sqlcomm.Parameters["@lLevel"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lLevel"].Value = level;
sqlcomm.Parameters.Add("@lSource", SqlDbType.NVarChar, 100);
sqlcomm.Parameters["@lSource"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lSource"].Value = source;
sqlcomm.Parameters.Add("@lException", SqlDbType.NVarChar, 100);
sqlcomm.Parameters["@lException"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lException"].Value = exception;
sqlcomm.Parameters.Add("@lType", SqlDbType.Int);
sqlcomm.Parameters["@lType"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lType"].Value = type;
sqlcomm.Parameters.Add("@lDate", SqlDbType.DateTime);
sqlcomm.Parameters["@lDate"].Direction = ParameterDirection.Input;
sqlcomm.Parameters["@lDate"].Value = DateTime.Now;
return sqlcomm.ExecuteNonQuery();
}
public void Close()
{
this.Info("Log End", 5);
_sqlConn.Close();
}
信息[Info]
警告[Warn]
调试[Debug]
错误[Error]
致命错误[Fatal]
}
}
文件FileLog.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
namespace LZ2007.Function.Log
{
/// <summary>
/// FileLog 的摘要说明
/// </summary>
///
public class FileLog : ILog
{
私有变量申明
public FileLog(Type objType)
{
_isDebug = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogDebug"]);
_isInfo = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogInfo"]);
_isError = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogError"]);
_isWarn = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogWarn"]);
_LogObjectSource = objType.FullName;
_LogFile = ConfigurationManager.AppSettings["eLogFile"];
_LogMsgFormat = ConfigurationManager.AppSettings["eLogMsgFormat"];
Init();
}
public void Close()
{
this.Info("Log End", 5);
sw.Close();
}
私有函数实现
信息[Info]
警告[Warn]
调试[Debug]
错误[Error]
致命错误[Fatal]
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
namespace LZ2007.Function.Log
{
/// <summary>
/// FileLog 的摘要说明
/// </summary>
///
public class FileLog : ILog
{
私有变量申明
public FileLog(Type objType)
{
_isDebug = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogDebug"]);
_isInfo = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogInfo"]);
_isError = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogError"]);
_isWarn = Convert.ToBoolean(ConfigurationManager.AppSettings["eLogWarn"]);
_LogObjectSource = objType.FullName;
_LogFile = ConfigurationManager.AppSettings["eLogFile"];
_LogMsgFormat = ConfigurationManager.AppSettings["eLogMsgFormat"];
Init();
}
public void Close()
{
this.Info("Log End", 5);
sw.Close();
}
私有函数实现
信息[Info]
警告[Warn]
调试[Debug]
错误[Error]
致命错误[Fatal]
}
}
配置文件Web.config
<appSettings>
<add key="eLogConnStr" value="Data Source=LZSHSQLSERVER2005;Initial Catalog=LZDB;Persist Security Info=True;User ID=sa;password=oilchem2007;" />
<add key="eLogFile" value="sys.log" />
<add key="eLogType" value="1" />
<add key="eLogDebug" value="False" />
<add key="eLogInfo" value="True" />
<add key="eLogError" value="True" />
<add key="eLogWarn" value="True" />
<add key="eLogMsgFormat" value="{$type}[{$time}]:{$message}:({$level}) --- {$source}[{$exception}]"/>
</appSettings>
<add key="eLogConnStr" value="Data Source=LZSHSQLSERVER2005;Initial Catalog=LZDB;Persist Security Info=True;User ID=sa;password=oilchem2007;" />
<add key="eLogFile" value="sys.log" />
<add key="eLogType" value="1" />
<add key="eLogDebug" value="False" />
<add key="eLogInfo" value="True" />
<add key="eLogError" value="True" />
<add key="eLogWarn" value="True" />
<add key="eLogMsgFormat" value="{$type}[{$time}]:{$message}:({$level}) --- {$source}[{$exception}]"/>
</appSettings>
测试代码:
private static ILog log = LogFactory.GetLog(typeof(Test));
protected void Page_Load(object sender, EventArgs e)
{
int i = 10, j = 0, k;
try
{
log.Info("记录一下信息!");
k = i / j;
}
catch (Exception e1)
{
log.Error("发生错误1", e1);
}
finally
{
log.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
int i = 10, j = 0, k;
try
{
log.Info("记录一下信息!");
k = i / j;
}
catch (Exception e1)
{
log.Error("发生错误1", e1);
}
finally
{
log.Close();
}
}
最后附上全部代码:
/Files/eicesoft/Log.zip