惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 叶小钗
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
爱范儿
爱范儿
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
SecWiki News
SecWiki News
MyScale Blog
MyScale Blog
AI
AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 【当耐特】
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Project Zero
Project Zero
T
Tor Project blog
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
IT之家
IT之家
The Hacker News
The Hacker News
腾讯CDC
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
C
Cisco Blogs
博客园 - 聂微东
Webroot Blog
Webroot Blog
Forbes - Security
Forbes - Security
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans

博客园 - 雨中流泪

轻量的PHP开发框架--- litePhp Rails 命令大全 ObjectHTML Framework 0.0.9.1 for PHP 发布.欢迎试用 在学习中沉沦... 在Dom4j中使用xpath - 雨中流泪 - 博客园 Spring.Net + NHibernate 入门例子 - 雨中流泪 使用Spring 2.5 和 Hibernate 3.2 开发MVC Web程序(基于annotation特性) PHP函数收藏---不断更新中! - 雨中流泪 - 博客园 程序员的编辑器--EmEditor 自己写的一个JS组件,JCombo 0.9 - 雨中流泪 - 博客园 Enterprise Library 3.1的研究之路---Data Access Application Block(1) 发个"违法"的东西.VisualAssist X 1559破解版 (三)AJAXPro之旅---原理的探究 - 雨中流泪 - 博客园 (二)AJAXPro之旅---构造实际的AJAX应用. (一)AJAXPro之旅---神奇的小魔盒 Windows Live Writer 我的AJAX类的使用(一个简单的异步组件) Grove,.Net下最方便的O/R Map库 ASP.Net分页组件1.0开发下载了...
自己写的Log日志记录类,支持文件和数据库,自动建立Log表格
雨中流泪 · 2007-09-12 · via 博客园 - 雨中流泪

自己写的 Log日志记录类,支持文件和数据库,自动建立Log表格,刚学设计模式,大家别见笑。

文件:ILog.cs代码

文件LogManage.cs代码

文件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
    
{
        
私有变量申明

        
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代码

配置文件Web.config

测试代码:

最后附上全部代码:
/Files/eicesoft/Log.zip