using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Web; namespace Com.AppCode.Helper
{
/// <summary>
/// 日志记录到本地文件
/// </summary>
public class Log
{
/// <summary>
/// 调试日志
/// </summary>
/// <param name="msg"></param>
static public void Debug(object msg)
{
WriteLog("Debug", string.Empty, msg);
}
/// <summary>
/// 错误或异常日志
/// </summary>
/// <param name="msg"></param>
static public void Error(object msg)
{
WriteLog("Error", string.Empty, msg);
}
/// <summary>
/// 内容日志
/// </summary>
/// <param name="msg"></param>
static public void Info(object msg)
{
WriteLog("Info", string.Empty, msg);
}
/// <summary>
/// 警告日志
/// </summary>
/// <param name="msg"></param>
static public void Warn(object msg)
{
WriteLog("Warn", string.Empty, msg);
} /// <summary>
/// 微信日志
/// </summary>
static public void Mp(object msg)
{
WriteLog("Mp", string.Empty, msg);
} static private Mutex m_Mutex = new Mutex();
/// <summary>
/// 日志文件记录
/// </summary>
/// <param name="errorType">日志类型</param>
/// <param name="pre">文件前缀可不填写</param>
/// <param name="msg">日志内容</param>
static public void WriteLog(object errorType, object pre, object msg)
{
try
{
m_Mutex.WaitOne();
}
catch (Exception)
{
return;
}
try
{
string dir = "/log/" + errorType + "/";
string dirPath = HttpContext.Current.Server.MapPath(dir);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
string fileName = pre + DateTime.Today.ToString("yyyyMMdd") + ".log";
string logPath = Path.Combine(dirPath, fileName);
if (!File.Exists(logPath))
{
File.Create(logPath).Close();
}
TextWriter oWrite = File.AppendText(logPath);
string sTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff");
oWrite.WriteLine(sTime + ": " + msg);
oWrite.Close();
}
catch (Exception e)
{
EventLog myLog = new EventLog();
myLog.Source = "Com.Log";
myLog.WriteEntry("Write Log Error:" + msg + "\t" + e.Message);
}
finally
{
m_Mutex.ReleaseMutex();
}
}
}
}

C#记录日志到本地文件工具类的更多相关文章

  1. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  2. Android FileUtil(android文件工具类)

    android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...

  3. 拍照、本地图片工具类(兼容至Android7.0)

    拍照.本地图片工具类:解决了4.4以上剪裁会提示"找不到文件"和6.0动态授予权限,及7.0报FileUriExposedException异常问题. package com.hb ...

  4. Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...

  5. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

  6. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  7. Java 实现删除文件工具类

    工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...

  8. HTTP 下载文件工具类

    ResponseUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.File; im ...

  9. java文件工具类

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

随机推荐

  1. java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the server

    错误信息 java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents ...

  2. .prop() vs .attr()

    .prop() vs .attr() Update 1 November 2012 My original answer applies specifically to jQuery 1.6. My ...

  3. C#Winform ListView中没有Item双击事件的两种实现方法!

    第一种: //if (this.listView1.FocusedItem != null) //{ // if (this.listView1.SelectedItems != null) // { ...

  4. Locust - A modern load testing framework https://locust.io/

    Locust - A modern load testing frameworkhttps://locust.io/

  5. jquery页面滚动到指定id

    //jquery页面滚动到指定id  $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html ...

  6. selenium元素input的value值设置【node.js版本】

    driver.executeScript(‘document.getElementById(“id”).value=“value”’); 这个操作就类似于//$("#id").va ...

  7. 教你如何在linux上装逼,shell中颜色的设置

    linux启动后环境变量加载的顺序为:etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/bashrc] 想 ...

  8. ubuntu 18.04下greenplum安装笔记(一)Linux下基础环境的搭建

    背景 需要构建一个用于数据仓库的分布式数据库集群. 每一个节点暂时不需要进行备份,同时也不考虑坏掉的情况. 每一个数据节点最好都不用进行过多的配置,安装起来方便. Greenplum的Shared-N ...

  9. maven项目创建.m2文件夹

    创建为.m2.,m2前后都要有点,然后去掉后面的点 settings.xml文件如下: <?xml version="1.0" encoding="UTF-8&qu ...

  10. react用redux 做的todolist

    ### 1. 创建项目  create - react - app  项目名(shop) ### 2. 进入项目,下载redux  cnpm install redux  --save  ### 3. ...