美文网首页通往成功之路
c#写日志(.text)

c#写日志(.text)

作者: 此十八 | 来源:发表于2018-06-06 15:11 被阅读13次

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

namespace BMCMInterface

{

    public class LogFiles

    {

        private static string _fileName;

        private static Dictionary lockDic = new Dictionary();

        ///        ///获取或设置文件名称        ///        public string FileName

        {

            get { return _fileName; }

            set { _fileName = value; }

        }

        ////        ///构造函数        ///        ///每次开辟位数大小,这个直接影响到记录文件的效率        ///文件全路径名        public LogFiles(string fileName)

        {

            _fileName = fileName;

        }

        ///        ///构造函数        ///        public LogFiles()

        { }

        ///        ///创建文件        ///        ///        public void Create()

        {

            if (System.IO.File.Exists(_fileName))

            {

                FileInfo logfileinfo = new FileInfo(_fileName);

                if (logfileinfo.Length > 10240000)

                {

                    GenerateNewLogFileName();

                }

            }

        }

        ///        ///写入文本        ///        ///文本内容        private void Write(string content, string newLine, string time)

        {

            if (string.IsNullOrEmpty(_fileName))

            {

                GenerateNewLogFileName();

            }

            Create();

            using (System.IO.FileStream fs = new System.IO.FileStream(_fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite, 8, System.IO.FileOptions.Asynchronous))

            {

                //Byte[] dataArray = System.Text.Encoding.ASCII.GetBytes(System.DateTime.Now.ToString() + content + "/r/n");                Byte[] dataArray = System.Text.Encoding.Default.GetBytes(time + "  " + content + newLine);

                bool flag = true;

                long slen = dataArray.Length;

                long len = 0;

                while (flag)

                {

                    try                    {

                        if (len >= fs.Length)

                        {

                            fs.Lock(len, slen);

                            lockDic[len] = slen;

                            flag = false;

                        }

                        else                        {

                            len = fs.Length;

                        }

                    }

                    catch (Exception ex)

                    {

                        while (!lockDic.ContainsKey(len))

                        {

                            len += lockDic[len];

                        }

                    }

                }

                fs.Seek(len, System.IO.SeekOrigin.Begin);

                fs.Write(dataArray, 0, dataArray.Length);

                fs.Close();

            }

        }

        ///        ///写入文件内容        ///        ///        public void WriteLine(string content)

        {

            this.Write(content, System.Environment.NewLine, DateTime.Now.ToString());

        }

        ///        ///写入文件        ///        ///        public void Write(string content)

        {

            this.Write(content, "", "");

        }

        ///        ///构造log文件名        ///        private void GenerateNewLogFileName()

        {

            //generate a new log file name            string strDateTimeString = string.Format("{0:yyyyMMddHHmm}", System.DateTime.Now);

            string LogFile_Name = strDateTimeString + ".log";

            _fileName = AppDomain.CurrentDomain.BaseDirectory + LogFile_Name;

        }

    }

}

//赋值:LogFile.FileName =AppDomain.CurrentDomain.BaseDirectory + "logText.log"; //调用:LogFile.WriteLine("获取异常:" + ex.Message + sql);

相关文章

  • c#写日志(.text)

    using System;using System.Collections.Generic;using Syste...

  • 正则验证

    C#正则验证手机号: bool isMobile = System.Text.RegularExpressions...

  • C#笔记-1(WPF)

    C#笔记之一(WPF) Xaml部分 对于有Text或Content属性的控件,想要使用例如Segoe MDL2 ...

  • CSHotFix热更新方案修复Unity3D游戏 c# bug和

    先上几张图: 方案亮点:用c#写战斗逻辑,c#热更新战斗bug,c#写成长逻辑,c#完全热更新成长逻辑。也就是整个...

  • NLog 实现日志输出到文件详细步骤

    创建一个简单的Demo C# Project,演示如果创建日志文件以及如何将日志内容输出文件。 1. 添加NLog...

  • ES 日志配置

    ES 日志配置 目前版本(7.x),ES Server本身同时支持两种类型的日志,即json类型和text类型。 ...

  • JNI错误排查

    保存log日志: adb logcat > log.text ndk-stack 工具分析: adb logcat...

  • 创业前记录的意义

    ETLOG ETLOG (Entrepreneurs Text Log) 创业日志。 不知道这个名字起的对不对,如...

  • 为了顶照片而写的日志

    为了顶照片而写的日志为了顶照片而写的日志为了顶照片而写的日志为了顶照片而写的日志为了顶照片而写的日志为了顶照片而写...

  • c#推特登录发推评论

    第一次写博客 用c#做了一个有关推特的模拟登录,评论等内容。 开始用c#写时,对c#也就是写过学校的数据库实验课设...

网友评论

    本文标题:c#写日志(.text)

    本文链接:https://www.haomeiwen.com/subject/adbbsftx.html