日志的记录是将程序过程中的一些行为数据记录下来,方便开发、运维迅速的找到问题的所在,节省时间。使用时在

站点的web.config 中的<appSettings></appSettings>节点中增加:

  <!-- 日志路径 -->

  <add key="LogDirectory" value="E:\TextLog" />

  <!-- 日志名称 -->

  <add key="ProgramName" value="日志记入类" />

private static string LogDirectory = ConfigurationManager.AppSettings["LogDirectory"];
private static string programName = ConfigurationManager.AppSettings["ProgramName"];
/// <summary>
/// 是否入库 web.config配置 UniteLogStart
/// </summary>
//private static string isInLog = ConfigurationManager.AppSettings["UniteLogStart"];
private static string isInLog;
public LogProxy()
{ } /// <summary>
/// 重载功能操作日志入库
/// </summary>
/// <param name="text">错误信息</param>
/// <param name="userid">用户名</param>
/// <param name="scode">功能id</param>
public static void Write(string text, string userid, string scode, Database m_DataBase)
{
try
{
if (isInLog != null || isInLog == "")//是否入库
{
//插入该操作
string tmpSQL = String.Format("insert into SYS_USER_LOG(USER_ID,CLIENT_IP,BUSINESS_NAME,BUSINESS_ID,IS_SUCCESS) values('{0}','{1}',F_GET_BUSINESSNAME('{2}'),'{2}','1')", userid, HttpContext.Current.Request.UserHostAddress, scode);
m_DataBase.ExecuteNonQuery(m_DataBase.GetSqlStringCommand(tmpSQL));
}
}
catch (Exception e)
{
WriteExp("操作日志插入错误:" + e.Message);
} string directory = GetFullDirectory();
string fileName = Thread.CurrentThread.Name + ".log";
LogWriter.Writer(directory, fileName, text); }
/// <summary>
/// 重载是否错误日志入库
/// </summary>
/// <param name="text">错误信息</param>
/// <param name="userid">用户名</param>
/// <param name="scode">功能id</param>
public static void WriteExp(string text, string userid, string scode, Database m_DataBase)
{ try
{
if (isInLog != null || isInLog == "")//是否入库
{
//插入该操作
string tmpSQL = String.Format("insert into SYS_USER_LOG(USER_ID,CLIENT_IP,BUSINESS_NAME,BUSINESS_ID,IS_SUCCESS,FAILURE_EXCEPTION) values('{0}','{1}',F_GET_BUSINESSNAME('{2}'),'{2}','1','{3}')", userid, HttpContext.Current.Request.UserHostAddress, scode, text);
m_DataBase.ExecuteNonQuery(m_DataBase.GetSqlStringCommand(tmpSQL));
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
string directory = GetFullDirectory();
string fileName = Thread.CurrentThread.Name + "_Exp.log";
LogWriter.Writer(directory, fileName, text);
} /// <summary>
/// 针对多线程的应用程序写日志
/// </summary>
/// <param name="text"></param>
public static void Write(string text)
{
string directory = GetFullDirectory();
string fileName = Thread.CurrentThread.Name + ".log";
LogWriter.Writer(directory, fileName, text); }
public static void WriteExp(string text)
{
string directory = GetFullDirectory();
string fileName = Thread.CurrentThread.Name + "_Exp.log";
LogWriter.Writer(directory, fileName, text);
}
public static void WriteDug(string text)
{
string directory = GetFullDirectory();
string fileName = Thread.CurrentThread.Name + "_Dug.log";
LogWriter.Writer(directory, fileName, text);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
public static void WriteExp(System.Exception e)
{
WriteExp(e.Message + "\r\n" + e.StackTrace);
}
public static void WriteTest(string text)
{
string directory = GetFullDirectory();
string fileName = Thread.CurrentThread.Name + "_test.log";
LogWriter.Writer(directory, fileName, text);
}
public static void WriteKeeper(string text, string keepersn)
{
string directory = GetFullDirectory();
string fileName = keepersn + ".log";
LogWriter.Writer(directory, fileName, text);
} /// <summary>
/// 针对单线程的应用程序写日志
/// </summary>
/// <param name="text"></param>
public static void STWrite(string text)
{
string directory = GetFullDirectory();
string fileName = programName + ".log";
LogWriter.Writer(directory, fileName, text);
}
public static void STWriteExp(string text)
{
string directory = GetFullDirectory();
string fileName = programName + "_Exp.log";
LogWriter.Writer(directory, fileName, text);
}
public static void STWrite(string filePrefixName, string text)
{
string directory = GetFullDirectory();
string fileName = filePrefixName + ".log";
LogWriter.Writer(directory, fileName, text);
}
public static void STWriteExp(string filePrefixName, string text)
{
string directory = GetFullDirectory();
string fileName = filePrefixName + "_Exp.log";
LogWriter.Writer(directory, fileName, text);
} private static string GetFullDirectory()
{
return LogDirectory + "\\"
+ DateTime.Now.ToString("yyyy-MM") + "\\" + DateTime.Now.Day.ToString()
+ "\\" + programName;
}
 internal class LogWriter
{ public LogWriter()
{ } [MethodImpl(MethodImplOptions.Synchronized)]
public static void Writer(string directory, string fileName, string text)
{
CheckDirectory(directory);
using (StreamWriter sw = new StreamWriter(directory + "\\" + fileName, true, Encoding.UTF8))
{
sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss "));
sw.WriteLine(" " + text);
sw.WriteLine("-------------------------------------------");
}
}
private static void CheckDirectory(string directory)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
}

以上是两个类库的代码。在程序中使用:

 LogProxy.WriteExp("Come in ");

C#通用类库整理--日志记录的更多相关文章

  1. 【C#通用类】日志记录类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  2. C#通用类库整理--字符串处理类

    在程序开发中通常需要将字符串转为自己想要的结果,以下三个类库主要实现: 1.GetStrArray(string str, char speater, bool toLower)  把字符串按照分隔符 ...

  3. C#通用类库整理--序列化类

    程序员在编写应用程序的时候往往要将程序的某些数据存储在内存中,然后将其写入某个文件或是将它传输到网络中的另一台计算机上 以实现通讯.这个将程序数据转化成能被存储并传输的格式的过程被称为"序列 ...

  4. C# 面向切面编程--监控日志记录方案

    背景:现在公司整体在做监控平台,要求把各个部分的细节都记录下来,在前台页面上有所显示,所以现在需要做的就是一个监控日志的记录工作,今天讲的就是渲染监控日志的例子. 现状:当前的渲染程序没有为监控日志记 ...

  5. 日志记录类库log4net的使用总结

    log4net是一个开源的日志记录类库,经过配置后可以自动抓取程序中的错误.异常信息,并写入磁盘,也可以在异常发生时执行其他指定的操作,比如:通知某人右键.写入数据库等.这里写个ASP.NET MVC ...

  6. 类库里面添加日志记录 log4net

    第一步: 新建一个公共类库common,添加CustomLog4jLogger.cs 并引用log4net.dll /// <summary> /// 日志记录 /// </summ ...

  7. JAVA实现通用日志记录

    原文:http://blog.csdn.net/jinzhencs/article/details/51882751 前言: 之前想在filter层直接过滤httpServerletRequest请求 ...

  8. 【个人使用.Net类库】(2)Log日志记录类

    开发接口程序时,要保证程序稳定运行就要时刻监控接口程序发送和接收的数据,这就需要一个日志记录的类将需要的信息记录在日志文件中,便于自己维护接口程序.(Web系统也是如此,只是对应的日志实现比这个要复杂 ...

  9. 【干货】.NET开发通用组件发布(四) 日志记录组件

    组件介绍和合作开发 http://www.cnblogs.com/MrHuo/p/MrHuoControls.html 日志记录组件功能介绍 通过基类Logger,实现了文本记录日志和数据库记录日志两 ...

随机推荐

  1. java编写非对称加密,解密,公钥加密,私钥解密,RSA,rsa

    非对称加密已经被评为加密标准,主要包含(公钥加密私钥解密,或者私钥加密公钥解密)本文主要讲解的是如何用java生成 公钥和私钥并且 进行字符串加密 和字符串解密    //如需要代码copy如下 im ...

  2. LinkedHashMap源码解读

    1. 前言 还是从面试中来,到面试中去.面试官在面试 Redis 的时候经常会问到,Redis 的 LRU 是如何实现的?如果让你实现 LRU 算法,你会怎么实现呢?除了用现有的结构 LinkedHa ...

  3. Falsk 路由简析

    添加路由 我们熟知添加路由的方式是装饰器: @app.route('/') def hello_world(): return 'Hello World!' #访问web得到 'Hello World ...

  4. LeetCode专题——详解搜索算法中的搜索策略和剪枝

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题第20篇文章,今天讨论的是数字组合问题. 描述 给定一个int类型的候选集,和一个int类型的target,要求返 ...

  5. JAVAEE学习day05学习,数组

    容器及元素的概念 容器:是将多个数据存储到一起 元素:每个数据称为该容器的元素 数组的概念 数组:数组是长度固定,存储数据的容器,保证多个数据的类型要一致 数组定义格式及其描述 动态定义: 数据类型 ...

  6. XMind ZEN 2020 (10.0.2) 全平台 完美破解版(ubuntu)

    XMind ZEN 2020 (10.0.2) 全平台 完美破解版(ubuntu) https://www.ghpym.com/xmindzen.html/comment-page-8?replyto ...

  7. 多线程的CAS

    CAS Compare And Swap (Compare And Exchange) / 自旋 / 自旋锁 / 无锁 独占锁:独占锁是一种悲观锁,synchronized就是一种独占锁,会导致其它所 ...

  8. (11)nc命令(每周一个linux命令)

    nc(netcat)实用程序几乎可用于所有涉及TCP或UDP的事情.它可以打开TCP连接,发送UDP数据包,监听任意TCP和UDP端口,进行端口扫描,处理IPv4和IPv6.与telnet不同,nc可 ...

  9. 理解Golang组件protobuf

    什么是protobuf protocol buffers 是一种语言无关.平台无关.可扩展的序列化结构数据的方法,它可用于(数据)通信协议.数据存储等.是一种灵活,高效,自动化机制的结构数据序列化方法 ...

  10. dfs 例题皇后问题

    题目描述 一个如下的 6 \times 66×6 的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列  ...