日志辅助类

using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;   

 

/// <summary>
    /// 本地日志辅助类
    /// </summary>
    public class LocalLogHelper
    {
        /// <summary>
        /// 写日志方法
        /// </summary>
        /// <param name="logstring">The logstring.</param>
        public static void WriteLog(string logstring)
        {
            Type type = typeof(LocalLogHelper);
            Monitor.Enter(type);
            try
            {
                string str = Path.Combine(Application.StartupPath, "Log");
                if (!Directory.Exists(str))
                {
                    Directory.CreateDirectory(str);
                }
                string fullName = Assembly.GetCallingAssembly().FullName;
                //string str1 = Path.Combine(str, string.Concat(fullName.Substring(0, fullName.IndexOf(",", StringComparison.Ordinal)), ".txt"));
                string str1 = Path.Combine(str, string.Concat(Application.ProductName, ".txt"));
                FileInfo logFile = new FileInfo(str1);
                if (File.Exists(str1) && logFile.Length > 2000000)  //如果日志文件大于该值则另存为.bak文件
                {
                    File.Copy(str1, string.Concat(str1 + DateTime.Now.ToString("yyyyMMdd"), ".bak"), true); 
                    File.Delete(str1);
                }
                StreamWriter streamWriter = null;
                try
                {
                    streamWriter = new StreamWriter(str1, true, Encoding.GetEncoding("gb2312"));
                    DateTime now = DateTime.Now;
                    streamWriter.WriteLine(string.Concat("时间:", now.ToString("G")));
                    //string fullName = Assembly.GetCallingAssembly().FullName;
                    int hashCode = Assembly.GetCallingAssembly().GetHashCode();
                    streamWriter.WriteLine(string.Concat("调用模块:", fullName, hashCode.ToString()));
                    streamWriter.WriteLine(string.Concat("信息:", logstring));
                    streamWriter.WriteLine();
                }
                finally
                {
                    streamWriter?.Close();
                }
            }
            finally
            {
                Monitor.Exit(type);
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值