NLog使用

LogHelper.cs

using NLog;
using NLog.Targets;

namespace MyProject.Tool.Log
{
    public class LogHelper
    {
        readonly FileTarget _target;
        readonly Logger _logger;

        static LogHelper _instance;
        private static readonly object m_syncRoot = new object();

        public static LogHelper GetInstance()
        {
            if (_instance == null)
            {
                lock (m_syncRoot)
                {
                    if (_instance == null)
                    {
                        _instance = new LogHelper();
                    }
                }
            }

            return _instance;
        }

        private LogHelper()
        {
            //初始化 NLog 的一些信息
            _target = new FileTarget();
            _target.Layout = "${longdate} ${logger} ${message}";
            _target.FileName = "${basedir}/log/${shortdate}.log";
            _target.KeepFileOpen = false;

            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(_target, LogLevel.Debug);

            _logger = LogManager.GetLogger("TeMai:");
        }

        /// <summary>
        /// 写日志信息
        /// </summary>
        /// <param name="message">具体的消息内容</param>
        public void WriteMessage(string message)
        {
            _logger.Debug(message);
        }
    }
}

LogHelper.GetInstance().WriteMessage("UserInfoClient---UserInfoLogin:" + ex.Message);

日志插件对比

https://dotnet.libhunt.com/project/log4net/vs/semantic-logging?rel=cmp-cmp

日志记录插件源码:

 https://github.com/NLog/NLog

https://github.com/serilog/serilog

https://github.com/apache/log4net

https://github.com/elmah/Elmah

https://github.com/logary/logary

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值