zoukankan      html  css  js  c++  java
  • 一个不需要Log4Net的写日志的简单方法

    有些项目写日志时会选择大名鼎鼎的Log4Net。而在我们使用它时,总会出现一些诸如版本不匹配而造成的写日志失败的情况,还要改web.config,还要改AssemblyInfo。而且,它的失败,并不是以日志的形式展现,而是“无反应”,你无法知道是哪里出了问题,最终的效果就是“没有输出日志且不知道为什么,需要根据百度和经验判断”。索性放弃。我只是要输出文本日志而已,杀鸡不要用牛刀了。

    以下是一个简单实用的日志类,无需配置。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.IO;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Web;
     7 
     8 namespace  Common
     9 {
    10    public class LogHelper
    11     {
    12 
    13        /// <summary>
    14        /// 写入日志
    15        /// </summary>
    16         ///  <param name="action">日志说明</param>
    17        /// <param name="msg">内容</param>
    18         public static void WriteLog(string action,string msg)
    19         {
    20             string sj=DateTime.Now.ToString("yyyyMMdd");
    21             string logFileName = action+sj+ ".txt";
    22 
    23 
    24            string logPath = HttpContext.Current.Server.MapPath("~/Log/"+sj+"");
    25             string fullPath = logPath + @"" + logFileName;
    26             
    27             if (!Directory.Exists(logPath))
    28             {
    29                 Directory.CreateDirectory(logPath);
    30             }
    31             using (StreamWriter writer = File.AppendText(fullPath))
    32             {
    33                 Log(msg, writer);
    34                 writer.Close();
    35             }
    36         }
    37         private static void Log(string logMessage, TextWriter writer)
    38         {
    39             writer.Write("
    Log Entry : ");
    40             writer.WriteLine("{0} {1} :", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
    41             writer.WriteLine(" {0}", logMessage);
    42             writer.WriteLine("-------------------------------");
    43             writer.Flush();
    44         }
    45     }
    46 }
  • 相关阅读:
    Silverlight DataGrid 获取 Row 左键双击事件
    数据结果集拼接到一行
    程序“[6040] iisexpress.exe”已退出,返回值为 0 (0x0)。
    新手用WPF山寨QQ管家7.6(二)
    风向十六方位图和温度湿度图
    新手向使用XAML画出Win8风格图标的照相机,小姐你相机~~
    新手用WPF山寨QQ管家7.6(一)
    实验一
    实验5
    实验4
  • 原文地址:https://www.cnblogs.com/apeng/p/12941121.html
Copyright © 2011-2022 走看看