C# 写日志帮助类

写日志帮助类,共有三个静态方法。

1.创建错误日志,写文本文件。

2.创建错误日志,写入MSSQL数据库。

2.创建登陆日志,写入MSSQL数据库。

using System;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace myClass
{
public class clsLogHelper
{

#region 创建错误日志
///-----------------------------------------------------------------------------
/// <summary>创建错误日志 在c:\ErrorLog\</summary>
/// <param name="strFunctionName">strFunctionName,调用方法名</param>
/// <param name="strErrorNum">strErrorNum,错误号</param>
/// <param name="strErrorDescription">strErrorDescription,错误内容</param>
/// <returns></returns>
/// <history>2009-05-29 Created</history>
///-----------------------------------------------------------------------------
//举例:
// try
// { 要监视的代码 }
// catch()
// { myErrorLog.m_CreateErrorLogTxt("myExecute(" & str执行SQL语句 & ")", Err.Number.ToString, Err.Description) }
// finally
// { }
public static void m_CreateErrorLogTxt(string strFunctionName, string strErrorNum, string strErrorDescription)
{
string strMatter; //错误内容
string strPath; //错误文件的路径
DateTime dt = DateTime.Now;
try
{
//Server.MapPath("./") + "File"; 服务器端路径
strPath = Directory.GetCurrentDirectory() + "\\ErrorLog"; //winform工程\bin\目录下 创建日志文件夹
//strPath = "c:" + "\\ErrorLog";//暂时放在c:下

if(Directory.Exists(strPath)==false) //工程目录下 Log目录 '目录是否存在,为true则没有此目录
{
Directory.CreateDirectory(strPath);
//建立目录 Directory为目录对象
}
strPath
= strPath + "\\" + dt.ToString("yyyyMM");

if(Directory.Exists(strPath) == false) //目录是否存在 '工程目录下 Log\月 目录 yyyymm
{
Directory.CreateDirectory(strPath);
//建立目录//日志文件,以 日 命名
}
strPath
= strPath + "\\" + dt.ToString("dd") + ".txt";

strMatter
= strFunctionName + " , " + strErrorNum + " , " + strErrorDescription;//生成错误信息

StreamWriter FileWriter
= new StreamWriter(strPath, true); //创建日志文件
FileWriter.WriteLine("Time: " + dt.ToString("HH:mm:ss") + " Err: " + strMatter);
FileWriter.Close();
//关闭StreamWriter对象
}
catch(Exception ex)
{
//("写错误日志时出现问题,请与管理员联系! 原错误:" + strMatter + "写日志错误:" + ex.Message.ToString());
string str=ex.Message.ToString();
}
}
#endregion

#region 创建错误日志
///-----------------------------------------------------------------------------
/// <summary>创建错误日志 在 数据库 sys_ErrorLog 表中</summary>
/// <param name="strFunctionName">strFunctionName,调用方法名</param>
/// <param name="strErrorNum">strErrorNum,错误号</param>
/// <param name="strErrorDescription">strErrorDescription,错误内容</param>
/// <returns></returns>
/// <history>2009-09-08 Created</history>
///-----------------------------------------------------------------------------
//举例:
// try
// { 要监视的代码 }
// catch()
// { myErrorLog.m_CreateErrorLogSql("myExecute(" & str执行SQL语句 & ")", Err.Number.ToString, Err.Description) }
// finally
// { }
//重载--默认连接字符串
public static void m_CreateErrorLogSql(string strFunctionName, string strErrorNum, string strErrorDescription)
{
m_CreateErrorLogSql(clsPubConstant.p_ConnectionString, strFunctionName, strErrorNum, strErrorDescription);
}
public static void m_CreateErrorLogSql(string strConnectionString, string strFunctionName, string strErrorNum, string strErrorDescription)
{
try
{
DbHelperSQL.connectionString
= strConnectionString;//连接字符串
if (strFunctionName.Length >= 90)//控件字符串长度
{
strFunctionName
= strFunctionName.Remove(90);
}
if (strErrorNum.Length >= 90)
{
strErrorNum
= strErrorNum.Remove(90);
}
if (strErrorDescription.Length >= 190)
{
strErrorDescription
= strErrorDescription.Remove(190);
}

StringBuilder strSql
= new StringBuilder();
strSql.Append(
"insert into sys_ErrorLog(");
strSql.Append(
"DateTime,FunctionName,ErrorNum,ErrorDescription)");
strSql.Append(
" values (");
strSql.Append(
"@DateTime,@FunctionName,@ErrorNum,@ErrorDescription)");
SqlParameter[] parameters
= {
new SqlParameter("@DateTime", SqlDbType.NVarChar),
new SqlParameter("@FunctionName", SqlDbType.NVarChar),
new SqlParameter("@ErrorNum", SqlDbType.NVarChar),
new SqlParameter("@ErrorDescription", SqlDbType.NVarChar)};
parameters[
0].Value = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//当前日间
parameters[1].Value = strFunctionName;
parameters[
2].Value = strErrorNum;
parameters[
3].Value = strErrorDescription;

DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
//执行
}
catch
{

}
}
#endregion

#region 创建登陆日志
///-----------------------------------------------------------------------------
/// <summary>创建登陆日志 在 数据库 sys_LoginLog 表中</summary>
/// <param name="strLoginDate">登陆时间</param>
/// <param name="strLoginName">登陆用户名</param>
/// <param name="strLoginStatus">登陆情况</param>
/// <param name="strEmployeeNo">员工编号</param>
/// <param name="strRemark">备注</param>
/// <returns></returns>
/// <history>2010-01-31 Created</history>
///-----------------------------------------------------------------------------
//举例:
// clsLogHelper.m_CreateLoginLogSql("2010-01-31 12:12:00", "admin", "登陆成功","1001","备注-预留字段") }
//重载--默认连接字符串
public static void m_CreateLoginLogSql(string strLoginName, string strLoginStatus, string strEmployeeNo, string strRemark)
{
m_CreateLoginLogSql(clsPubConstant.p_ConnectionString, strLoginName, strLoginStatus, strEmployeeNo, strRemark);
}
public static void m_CreateLoginLogSql(string strConnectionString,string strLoginName, string strLoginStatus, string strEmployeeNo, string strRemark)
{
try
{
DbHelperSQL.connectionString
= strConnectionString;//连接字符串

StringBuilder strSql
= new StringBuilder();
strSql.Append(
"insert into sys_LoginLog(");
strSql.Append(
"LoginDate,LoginName,LoginStatus,EmployeeNo,Remark)");
strSql.Append(
" values (");
strSql.Append(
"@LoginDate,@LoginName,@LoginStatus,@EmployeeNo,@Remark)");
SqlParameter[] parameters
= {
new SqlParameter("@LoginDate", SqlDbType.NVarChar),
new SqlParameter("@LoginName", SqlDbType.NVarChar),
new SqlParameter("@LoginStatus", SqlDbType.NVarChar),
new SqlParameter("@EmployeeNo", SqlDbType.NVarChar),
new SqlParameter("@Remark", SqlDbType.NVarChar)};
parameters[
0].Value = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//当前日间
parameters[1].Value = strLoginName;
parameters[
2].Value = strLoginStatus;
parameters[
3].Value = strEmployeeNo;
parameters[
4].Value = strRemark;

DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
//执行
}
catch(System.Exception ex)
{
m_CreateErrorLogSql(strConnectionString,
"m_CreateLoginLogSql( " + strLoginName + "," + strLoginStatus +"," + strEmployeeNo + "," + strRemark + ")", "", ex.Message.ToString());
}
}
#endregion
}
}
原文地址:https://www.cnblogs.com/hailexuexi/p/1910516.html