LogHelper 日志记录帮助类

简介: 1、LogHelper 日志记录帮助类 using System;using System.Collections.Generic;using System.

1、LogHelper 日志记录帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Weixin.Utils
{
    public enum LogType
    {
        Overall,
    }

    public class LogHelper
    {
        public static string LogPath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory+@"\log";
            }
        }

        public enum LogLevel
        {
            Info,
            Error
        }

        public static void Info(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "", message);
        }

        public static void Error(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", message);
        }

        public static void Error(Exception e, LogType logType = LogType.Overall)
        {
            if (e == null)
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", e.Message);
        }

        private static void WriteLog(string path, string prefix, string message)
        {
            path = LogPath + path;
            var fileName = string.Format("{0}{1}.log", prefix, DateTime.Now.ToString("yyyyMMdd"));

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            using (FileStream fs = new FileStream(path + fileName, FileMode.Append, FileAccess.Write,
                                                  FileShare.Write, 1024, FileOptions.Asynchronous))
            {
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("HH:mm:ss") + " " + message + "\r\n");
                IAsyncResult writeResult = fs.BeginWrite(buffer, 0, buffer.Length,
                    (asyncResult) =>
                    {
                        var fStream = (FileStream)asyncResult.AsyncState;
                        fStream.EndWrite(asyncResult);
                    },

                    fs);
                fs.Close();
            }
        }
    }
}


 

2、帮助类使用

 var msg = "日志文本信息";
  LogHelper.Info(msg);


 

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
2月前
|
Java 计算机视觉 Python
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进1】
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进1】
37 1
|
2月前
|
人工智能 算法 计算机视觉
我的自描外挂制作日志——FPS类游戏的自瞄【构思准备】
我的自描外挂制作日志——FPS类游戏的自瞄【构思准备】
43 0
|
3月前
|
前端开发
muduo源码剖析之AsyncLogging异步日志类
AsyncLogging是muduo的日志,程序如果直接让文件写日志可能会发生阻塞,muduo前端设计了2个BufferPtr,分别是currentBuffer_和nextBuffer_,还有一个存放BufferPtr的vector(buffers_)。多个前端线程往currentBuffer_写数据,currentBuffer_写满了将其放入buffers_,通知后端线程读。前端线程将currentBuffer_和nextBuffer_替换继续写currentBuffer_。
24 0
|
2月前
|
Java 计算机视觉
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进2】
我的自描外挂制作日志——FPS类游戏的自瞄【优化改进2】
21 0
|
2月前
|
计算机视觉
我的自描外挂制作日志——FPS类游戏的自瞄【验证猜想】
我的自描外挂制作日志——FPS类游戏的自瞄【验证猜想】
26 1
|
3月前
重写 AppiumService 类,添加默认启动参数,并实时显示启动日志
重写 AppiumService 类,添加默认启动参数,并实时显示启动日志
24 0
|
9月前
|
缓存 Python
python使用类装饰器生成函数的使用日志
在了解类装饰器之前,建议大家先了解装饰器的概念。 装饰器知识快速入门链接 类装饰器是 Python 中的一种特殊类型的装饰器,它是一个类而不是一个函数。与函数装饰器不同,类装饰器可以在运行时接收参数并返回一个可调用的对象,而不是直接替换被装饰的函数。
|
10月前
|
存储 SQL 缓存
MySQL-四大类日志
MySQL-四大类日志
|
10月前
|
编译器 C++
c++入门学习日志 -- 类 和 对象
c++入门学习日志 -- 类 和 对象
49 0
|
10月前
|
编译器 C语言 C++
C++服务器框架开发9——日志系统LogFormatter_4/各个类的关系梳理/std::function/std::get
C++服务器框架开发9——日志系统LogFormatter_4/各个类的关系梳理/std::function/std::get