Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用标准库在MVC中记录文本文件?_C#_Asp.net Mvc_Logging - Fatal编程技术网

C# 使用标准库在MVC中记录文本文件?

C# 使用标准库在MVC中记录文本文件?,c#,asp.net-mvc,logging,C#,Asp.net Mvc,Logging,我试图弄清楚如何在MVC中记录到一个平面txt文件 我不是在要求最好的解决方案。我要求的是一个正确的方向 我有一个想法,让一个单例类来处理我所有的日志记录 我基本上只想调用一个名为 alogger = Logger.GetLogger(); aLogger.Log("some message",enum.serverity) 每当我需要将某些内容记录到文件时。然后让singelton处理如何写入文件等等 但后来我开始查看microsoft.extensions.logging库,希望我可以将大

我试图弄清楚如何在MVC中记录到一个平面txt文件

我不是在要求最好的解决方案。我要求的是一个正确的方向

我有一个想法,让一个单例类来处理我所有的日志记录

我基本上只想调用一个名为

alogger = Logger.GetLogger();
aLogger.Log("some message",enum.serverity)
每当我需要将某些内容记录到文件时。然后让singelton处理如何写入文件等等

但后来我开始查看microsoft.extensions.logging库,希望我可以将大部分日志外包给NuGet中已有的一些标准

然而,我对这个库的复杂性感到不知所措,正在考虑只做我自己的基于线程的小日志记录。因为这似乎是我简单需求的简单解决方案。 有没有什么简单的指南可以帮助我归档我想要的东西,但仍然使用标准库,而这看起来不像我们正在构建一个火箭式的记录器?
有没有更好更简单的方法来解决这个问题

您所有的问题都有一个简单的答案-使用库

这是我在其中一个项目中完成的最简单的实现:

ILogger.cs

public interface ILogger
    {
        void Info(string message);

        void Warn(string message);

        void Debug(string message); 

        void Error(string message);

        void Error(Exception exception);

        void Fatal(string message);
    }
public class Logger : ILogger
    {
        #region Properties   
        private readonly ILog log;   //ILog refers to interface from log4net library

        //Singleton Instance of Logger Class
        private static readonly object threadLock = new Object();
        private static Lazy<Logger> instance;

        public static Logger Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (threadLock)
                    {
                        instance = new Lazy<Logger>(() => new Logger());
                        return instance.Value;
                    }
                }
                else
                {
                    return instance.Value;
                }
            }
        }

        #endregion

        #region Constructor
        //Private constructor to restrict from any instance creation
        private Logger()
        {
            XmlConfigurator.Configure();
            log = LogManager.GetLogger(GetType());
            //log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        }
        #endregion

        #region Methods
        public void Info(string message)
        {
            log.Info(message);
        }

        public void Warn(string message)
        {
            log.Warn(message); 
        }

        public void Debug(string message)  
        { 
            log.Debug(message);
        }

        public void Error(string message)
        {
            log.Error(message);
        }

        public void Error(Exception exception)
        {
            log.Error(exception);
        }

        public void Fatal(string message)
        {
            log.Fatal(message);
        }
        #endregion
    }
Logger.cs

public interface ILogger
    {
        void Info(string message);

        void Warn(string message);

        void Debug(string message); 

        void Error(string message);

        void Error(Exception exception);

        void Fatal(string message);
    }
public class Logger : ILogger
    {
        #region Properties   
        private readonly ILog log;   //ILog refers to interface from log4net library

        //Singleton Instance of Logger Class
        private static readonly object threadLock = new Object();
        private static Lazy<Logger> instance;

        public static Logger Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (threadLock)
                    {
                        instance = new Lazy<Logger>(() => new Logger());
                        return instance.Value;
                    }
                }
                else
                {
                    return instance.Value;
                }
            }
        }

        #endregion

        #region Constructor
        //Private constructor to restrict from any instance creation
        private Logger()
        {
            XmlConfigurator.Configure();
            log = LogManager.GetLogger(GetType());
            //log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        }
        #endregion

        #region Methods
        public void Info(string message)
        {
            log.Info(message);
        }

        public void Warn(string message)
        {
            log.Warn(message); 
        }

        public void Debug(string message)  
        { 
            log.Debug(message);
        }

        public void Error(string message)
        {
            log.Error(message);
        }

        public void Error(Exception exception)
        {
            log.Error(exception);
        }

        public void Fatal(string message)
        {
            log.Fatal(message);
        }
        #endregion
    }

你给我指出了一个好的方向:)我在Log4Net上做了一些研究,但最终使用了NLog。更简单的使用,更快,尽我所能的灵活。是的,你也可以使用NLog。两者都是相同的,但NLog拥有更积极的用户支持。干杯:)