前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >日志帮助类

日志帮助类

作者头像
用户1055830
发布2018-01-18 15:52:34
6300
发布2018-01-18 15:52:34
举报
文章被收录于专栏:飞扬的花生飞扬的花生

 1.代码

代码语言:javascript
复制
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 += "\r\n";
                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 += "\r\n";
                using (StreamWriter sw = new StreamWriter(logFile, true, Encoding.UTF8))
                {
                    sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
                }
            }
            catch { }
        }
    }
}

2.调用

代码语言:javascript
复制
 LogHelper.Common.LogHelper logHelper = new Common.LogHelper();

logHelper.WriteLine("你好");

3.路径临时配置位置

在当前位置

代码语言:javascript
复制
string logPath = Path.Combine(System.Environment.CurrentDirectory,DateTime.Now.ToString("yyyyMM"));
                logFile = Path.Combine(logPath,DateTime.Now.ToString("yyyyMMdd") + ".txt");

4.效果

代码语言:javascript
复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-02-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档