zoukankan      html  css  js  c++  java
  • c#日志生成

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    
    /// <summary>
    /// 打印error类
    /// </summary>
    public class LogUtil
    {
        private string path = string.Empty;
        private static Dictionary<long, long> lockDic = new Dictionary<long, long>();
        public LogUtil(string filePath, Enviroment enviroment)
        {
            switch (enviroment)
            {
                case Enviroment.HTTP:
                    path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/") + filePath;
                    break;
                case Enviroment.CLIENT:
                    path = Directory.GetCurrentDirectory() + "/" + filePath;
                    break;
                default:
                    break;
            }
    
            if (!Directory.Exists(path + "/Info"))
            {
                Directory.CreateDirectory(path + "/Info");
            }
    
            if (!Directory.Exists(path + "/Error"))
            {
                Directory.CreateDirectory(path + "/Error");
            }
    
            if (!Directory.Exists(path + "/Debug"))
            {
                Directory.CreateDirectory(path + "/Debug");
            }
        }
    
        private void Write(string path, string content)
        {
            if (!File.Exists(path))
            {
                using (FileStream fs = File.Create(path))
                {
                    fs.Close();
                }
            }
    
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 8, FileOptions.Asynchronous))
            { 
                Byte[] dataArray = Encoding.Default.GetBytes(content + Environment.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, SeekOrigin.Begin);
                fs.Write(dataArray, 0, dataArray.Length);
                fs.Close();
            }
        }
    
        /// <summary>
        /// 日志写入
        /// </summary>
        /// <param name="str">要写入的字符串</param>
        /// <param name="isAppend">是否是文本追加</param>
        public void Error(string str, bool isAppend = true)
        {
            Write(path + "/Error/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str);
        }
    
        /// <summary>
        /// 日志写入
        /// </summary>
        /// <param name="str">要写入的字符串</param>
        /// <param name="isAppend">是否是文本追加</param>
        public void Debug(string str, bool isAppend = true)
        {
    
            Write(path + "/Debug/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str);
        }
    
        /// <summary>
        /// 日志写入
        /// </summary>
        /// <param name="str">要写入的字符串</param>
        /// <param name="isAppend">是否是文本追加</param>
        public void Info(string str, bool isAppend = true)
        {
            Write(path + "/Info/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str);
        }
    
        /// <summary>
        /// 程序运行环境
        /// </summary>
        public enum Enviroment
        {
            /// <summary>
            /// webapi环境
            /// </summary>
            HTTP,
            /// <summary>
            /// 客户端
            /// </summary>
            CLIENT
        }
    }
  • 相关阅读:
    HTTP Continuation or nonHTTP traffic 数据包
    linuxTcp IP协议栈源码阅读笔记(转)
    使用Windows命令行启动服务
    数据库集群
    ShellExecute
    oracle 中数据库完全导入导出:cmd命令行模式
    理解ORACLE数据库字符集
    asp.net 编码设置
    ShellExecute与ShellExecuteEx的用法
    C++用位运算实现循环移位
  • 原文地址:https://www.cnblogs.com/Transmuter/p/14729241.html
Copyright © 2011-2022 走看看