自己写的Log日志记录类,支持文件和数据库,自动建立Log表格

自己写的 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();
    }

}

文件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;
        }

    }

}
文件DataBaseLog.cs代码
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
    
...{
        
私有变量申明#region 私有变量申明
        
private string _LogConnStr;
        
private string _LogObjectSource;

        
private bool _isDebug;
        
private bool _isInfo;
        
private bool _isError;
        
private bool _isWarn;

        
private SqlConnection _sqlConn;

        
public bool IsDebug
        
...{
            
get ...return _isDebug; }
            
set ...{ _isDebug = value; }
        }

        
public bool IsInfo
        
...{
            
get ...return _isInfo; }
            
set ...{ _isInfo = value; }
        }

        
public bool IsError
        
...{
            
get ...return _isError; }
            
set ...{ _isError = value; }
        }

        
public bool IsWarn
        
...{
            
get ...return _isWarn; }
            
set ...{ _isWarn = value; }
        }

        
#endregion


        
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();
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值