• c# 错误日志生成类


    public class LogHelper
    {
    #region 错误日志记录
    public static string webIniErrorLogName = HttpRuntime.AppDomainAppPath + "WebLog\E_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";//Web错误日志名称
    /// <summary>
    /// 创建日志目录
    /// </summary>
    private static void CreateDirectory()
    {
    if (!Directory.Exists(HttpRuntime.AppDomainAppPath + "WebLog"))
    {
    Directory.CreateDirectory(HttpRuntime.AppDomainAppPath + "WebLog");
    }
    }
    /// <summary>
    /// 创建错误日志文件
    /// </summary>
    private static void CreateMarsWebErrorLogFile()
    {
    CreateDirectory();
    if (!File.Exists(webIniErrorLogName))
    {
    string filename = webIniErrorLogName;//文件名称
    using (FileStream myFs = new FileStream(filename, FileMode.Create))
    {
    StreamWriter mySw = new StreamWriter(myFs);
    mySw.Write("");
    mySw.Dispose();
    myFs.Close();
    myFs.Dispose();
    }
    }
    }
    /// <summary>
    /// 写错误日志调用
    /// </summary>
    /// <param name="errorMsg"></param>
    public static void WriteMessageErrorLog(string errorMsg)
    {
    System.Threading.ParameterizedThreadStart logdele = new ParameterizedThreadStart(WriteMessageELog);
    AsyncCallback asyncCallback = new AsyncCallback(Async_ApplyStyleAndData);
    logdele.BeginInvoke(errorMsg, asyncCallback, null);
    }
    /// <summary>
    /// 写错误日志方法
    /// </summary>
    /// <param name="errorMsg"></param>
    private static void WriteMessageELog(object errorMsg)
    {
    CreateMarsWebErrorLogFile();
    int i = 0;
    do
    {
    try
    {
    string logFilePath = webIniErrorLogName;
    FileStream fs = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
    StreamWriter sw = new StreamWriter(fs);
    string context = " " + DateTime.Now + " " + errorMsg;
    byte[] contextByte = Encoding.UTF8.GetBytes(context);
    fs.Write(contextByte, 0, contextByte.Length);
    fs.Flush();
    fs.Close();
    fs.Dispose();
    break;
    }
    catch (Exception)
    {
    Thread.Sleep(500);
    }
    } while (i < 3);
    }
    /// <summary>
    /// 回调
    /// </summary>
    /// <param name="ar"></param>
    public static void Async_ApplyStyleAndData(IAsyncResult ar) { }
    /// <summary>
    /// 监测时间
    /// </summary>
    private void CheckTime()
    {
    if (DateTime.Now.Hour.ToString("00") == "00")
    {
    webIniErrorLogName = HttpRuntime.AppDomainAppPath + "WebLog\E_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";//Web错误日志名称
    }
    }
    /// <summary>
    /// 开始检测
    /// </summary>
    public void DoCheckTime()
    {
    while (true)
    {
    CheckTime();
    Thread.Sleep(180000);//半小时执行一次
    }
    }
    #endregion
    }

  • 相关阅读:
    百度搜索API v3版本与soap
    Yii整合ZF2及soap实例
    Getting started writing ZF2 modules
    js写出php中的函数系列
    一些有用的命令
    a标签至于flash之上的时候,IE浏览器无法点击连接的问题
    js问题集锦
    php常用函数集锦[备份用的]
    用过的一些js函数[备份用的]
    ELK
  • 原文地址:https://www.cnblogs.com/2260827114com/p/6479887.html
Copyright © 2020-2023  润新知