WPF下使用NLog

1 先从nuget中现在Nlog,由于我使用的是.net core 3.0 所以下载如图的Nlog 

2 创建Nlog.config配置文件


 
 
  

    

    
    
      
    

    
    
      
        
          INSERT INTO Log(Timestamp,Level,Message,Action,Amount,StackTrace) VALUES(@time_stamp, @level, @message, @action, @amount, @stacktrace);
        

        
        
        
        
        
        
        
      
    

    
    
    

    
    
  

  
    
     
 
    
  

3.写一个Logger类这里使用线程 以及线程锁。

using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Threading;

namespace com.aaa.Util
{
    public class Logger
    {
        static readonly object locker = new object();
        NLog.Logger _logger;

        private Logger(NLog.Logger logger)
        {
            _logger = logger;
        }

        public Logger(string name) : this(LogManager.GetLogger(name))
        {

        }

        public static Logger Default { get; private set; }
        static Logger()
        {
            Default = new Logger(NLog.LogManager.GetCurrentClassLogger());
        }

        public static void WriteLog1(string name, string msg)
        {
            LogModels lm = new LogModels();
            lm.Name = name;
            lm.msg = msg;

            ParameterizedThreadStart tStart = new ParameterizedThreadStart(ThreadWriteLog);
            Thread thread = new Thread(tStart);
            thread.Start(lm);//传递参数 
        }

        public static void ThreadWriteLog(object arg)
        {
            lock (locker)
            {
                LogModels lm = (LogModels)arg;
                LogManager.Configuration.Variables["LogDir"] = lm.Name;
                Logger logger = new Logger("ESDLog");
                logger.Info(lm.msg);
            }
        }

        #region Debug
        public void Debug(string msg, params object[] args)
        {
            _logger.Debug(msg, args);
        }

        public void Debug(string msg, Exception err)
        {
            _logger.Debug(err, msg);
        }
        #endregion

        #region Info
        public void Info(string msg, params object[] args)
        {
            _logger.Info(msg, args);
        }

        public void Info(string msg, Exception err)
        {
            _logger.Info(err, msg);
        }
        #endregion
        #region Custom
        #endregion

    }

    public class MyLogEventInfo : LogEventInfo
    {
        public MyLogEventInfo() { }
        public MyLogEventInfo(LogLevel level, string loggerName, string message) : base(level, loggerName, message)
        { }

        public override string ToString()
        {
            //Message format
            //Log Event: Logger='XXX' Level=Info Message='XXX' SequenceID=5
            return FormattedMessage;
        }
    }

    public class LogModels
    {
        public string Name { set; get; }
        public string msg { set; get; }
    }


}

 4.C#中使用日志记录

  1.写入Exception目录              
catch (Exception ex)
   { 
     string msg = string.Format("Exception  ={0} }", ex.Message);
     Logger.WriteLog1("Exception",msg); 
   }

  2.写入Exception\abc目录 
 catch (Exception ex)
     { 
        string msg = string.Format("Exception  ={0} }", ex.Message);
        Logger.WriteLog1(@"Exception\abc",msg); 
     }

 

 

你可能感兴趣的:(WPF,NLog)