/// <summary>
    /// 日志处理帮助类
    /// </summary>
    public class loghelper
    {
        private static queue<string> errorqueue = new queue<string>();
        static action<string> erroraction;
        static string logpath => system.web.hosting.hostingenvironment.mappath($"/app_data/log/{datetime.now.tostring("yyyymmdd")}.txt");
        static loghelper()
        {
            erroraction += writefile;
            task.run(() =>
            {
                while (true)
                {
                    if (errorqueue.count > 0)
                    {
                        lock (errorqueue)
                        {
                            var txt = errorqueue.dequeue(); //出队列
                            erroraction?.invoke(txt);   //写日志
                        }
                    }
                    else
                    {
                        task.delay(100);
                    }
                }
            });
        }

        /// <summary>
        /// 讲内容写入日志队列中
        /// </summary>
        /// <param name="errortext"></param>
        public static void writelog(string errortext)
        {
            lock (errorqueue)
            {
                errorqueue.enqueue(errortext); //进队列
            }
        }

        /// <summary>
        /// 写入文本文件
        /// </summary>
        /// <param name="txt"></param>
        static void writefile(string txt)
        {
            using (system.io.filestream fs = new system.io.filestream(logpath, system.io.filemode.append, system.io.fileaccess.write))
            {
                var bytestr = encoding.default.getbytes(txt);
                fs.write(bytestr, 0, bytestr.length);
            }
        }
    }

asp.net mvc日志处理帮助类

 

看到公司框架日志处理类写得~~~哎,不想说了。nuget装了log4net然而都不用。。。只能改造一下凑合用了