zoukankan      html  css  js  c++  java
  • 日志帮助类

     1.代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Configuration;
    using System.Reflection;
    
    namespace LogHelper.Common
    {
        public class LogHelper
        {
            private string logFile = "";
            /// <summary>
            /// 不带参数的构造函数
            /// </summary>
            public LogHelper()
            {
                try
                {
                    string logPath = Path.Combine(System.Environment.CurrentDirectory,DateTime.Now.ToString("yyyyMM"));
                    logFile = Path.Combine(logPath,DateTime.Now.ToString("yyyyMMdd") + ".txt");
                    if (!Directory.Exists(logPath))
                        Directory.CreateDirectory(logPath);
    
                    if (!File.Exists(logFile))
                        File.Create(logFile);
                }
                catch
                {
                }
            }
            /// <summary>
            /// 带参数的构造函数
            /// </summary>
            /// <param name="logFile"></param>
            public LogHelper(string logFile)
            {
                try
                {
                    this.logFile = logFile;
                    if (!File.Exists(logFile))
                    {
                        File.Create(logFile);
                    }
                }
                catch { }
    
            }
            /// <summary>
            /// 追加一条信息
            /// </summary>
            /// <param name="text"></param>
            public void Write(string text)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                    {
                        sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
                    }
                }
                catch { }
            }
            /// <summary>
            /// 追加一条信息
            /// </summary>
            /// <param name="logFile"></param>
            /// <param name="text"></param>
            public void Write(string logFile, string text)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                    {
                        sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
                    }
                }
                catch { }
            }
            /// <summary>
            /// 追加一行信息
            /// </summary>
            /// <param name="text"></param>
            public void WriteLine(string text)
            {
                try
                {
                    text += "
    ";
                    using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                    {
                        sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
                    }
                }
                catch { }
            }
            /// <summary>
            /// 追加一行信息
            /// </summary>
            /// <param name="logFile"></param>
            /// <param name="text"></param>
            public void WriteLine(string logFile, string text)
            {
                try
                {
                    text += "
    ";
                    using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                    {
                        sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
                    }
                }
                catch { }
            }
        }
    }

    2.调用

     LogHelper.Common.LogHelper logHelper = new Common.LogHelper();
    
    logHelper.WriteLine("你好");

    3.路径临时配置位置

    在当前位置

    string logPath = Path.Combine(System.Environment.CurrentDirectory,DateTime.Now.ToString("yyyyMM"));
                    logFile = Path.Combine(logPath,DateTime.Now.ToString("yyyyMMdd") + ".txt");

    4.效果




  • 相关阅读:
    xsos:一个在Linux上阅读SOSReport的工具
    RHEL sosreport
    sosreport -a --report
    环境变量
    读研重要的是要明白你自己要干什么, 不能等导师来告诉你你应该干什么. 研究生的优势在于理论功底深厚, 思维具有穿透力,
    awk sed grep 常用命令
    如何删除文件中的空行
    Vim删除空行
    WPS 2010 页眉下方添加下划线
    Android开发环境搭建
  • 原文地址:https://www.cnblogs.com/CallmeYhz/p/6380351.html
Copyright © 2011-2022 走看看