C# log日志

public class LogFileHelper
    {
        // Token: 0x06000B56 RID: 2902 RVA: 0x00034FC4 File Offset: 0x000331C4
        public static string CreatePath(string folder, string log)
        {
            string result;
            try
            {
                bool flag = _path == null;
                if (flag)
                {
                    UriBuilder uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase);
                    _path = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
                }
                string pathFolder = Path.Combine(_path, folder);
                bool flag2 = !Directory.Exists(Path.GetDirectoryName(pathFolder));
                if (flag2)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(pathFolder));
                }
                string strNow = DateTime.Now.ToString("yyyyMMdd");
                string strKey = pathFolder + strNow;
                int currentIndex;
                long size;
                bool flag3 = !_dictIndex.TryGetValue(strKey, out currentIndex) || !_dictSize.TryGetValue(strKey, out size);
                if (flag3)
                {
                    _dictIndex.Clear();
                    _dictSize.Clear();
                    GetIndexAndSize(pathFolder, strNow, out currentIndex, out size);
                    bool flag4 = size >= (long)_fileSize;
                    if (flag4)
                    {
                        currentIndex++;
                    }
                    _dictIndex.Add(strKey, currentIndex);
                    _dictSize.Add(strKey, size);
                }
                int index = _dictIndex[strKey];
                string logPath = Path.Combine(pathFolder, strNow + ((index == 1) ? "" : ("_" + index.ToString())) + ".txt");
                Dictionary<string, long> dictSize = _dictSize;
                string key = strKey;
                dictSize[key] += (long)Encoding.UTF8.GetByteCount(log);
                bool flag5 = _dictSize[strKey] > (long)_fileSize;
                if (flag5)
                {
                    Dictionary<string, int> dictIndex = _dictIndex;
                    key = strKey;
                    int num = dictIndex[key];
                    dictIndex[key] = num + 1;
                    _dictSize[strKey] = 0L;
                }
                result = logPath;
            }
            catch
            {
                result = null;
            }
            return result;
        }

        // Token: 0x06000B57 RID: 2903 RVA: 0x000351AC File Offset: 0x000333AC
        public static string ReadFromFile(string path)
        {
            bool flag = _path == null;
            if (flag)
            {
                UriBuilder uri = new UriBuilder(Assembly.GetExecutingAssembly().CodeBase);
                _path = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path));
            }
            string filePath = Path.Combine(_path, path);
            bool flag2 = string.IsNullOrEmpty(path) || !File.Exists(filePath);
            string result;
            if (flag2)
            {
                result = null;
            }
            else
            {
                try
                {
                    using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                catch (Exception)
                {
                    result = null;
                }
            }
            return result;
        }

        // Token: 0x06000B58 RID: 2904 RVA: 0x00035264 File Offset: 0x00033464
        public static void WriteFile(string log, string path)
        {
            try
            {
                FileStream fs;
                StreamWriter sw = null;
                bool flag = !_dictStream.TryGetValue(path, out fs) || !_dictWriter.TryGetValue(path, out sw);
                if (flag)
                {
                    foreach (StreamWriter item in _dictWriter.Values.ToList<StreamWriter>())
                    {
                        item.Close();
                    }
                    _dictWriter.Clear();
                    foreach (FileStream item2 in _dictStream.Values.ToList<FileStream>())
                    {
                        item2.Close();
                    }
                    _dictWriter.Clear();
                    bool flag2 = !Directory.Exists(Path.GetDirectoryName(path));
                    if (flag2)
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(path));
                    }
                    fs = new FileStream(path, FileMode.Append, FileAccess.Write);
                    sw = new StreamWriter(fs);
                    bool flag3 = !_dictWriter.ContainsKey(path);
                    if (flag3)
                    {
                        _dictWriter.Add(path, sw);
                    }
                    bool flag4 = !_dictStream.ContainsKey(path);
                    if (flag4)
                    {
                        _dictStream.Add(path, fs);
                    }
                }
                sw.WriteLine(log);
                sw.Flush();
                fs.Flush();
            }
            catch (Exception ex)
            {
                LogUtil.LogError(ex.Message);
            }
        }

        // Token: 0x06000B59 RID: 2905 RVA: 0x00035430 File Offset: 0x00033630
        private static void GetIndexAndSize(string pathFolder, string strNow, out int index, out long size)
        {
            index = 1;
            size = 0L;
            Regex regex = new Regex(strNow + "_*(\\d*).txt");
            string[] fileArr = Directory.GetFiles(pathFolder);
            string currentFile = null;
            foreach (string file in fileArr)
            {
                Match match = regex.Match(file);
                bool success = match.Success;
                if (success)
                {
                    string str = match.Groups[1].Value;
                    bool flag = !string.IsNullOrWhiteSpace(str);
                    if (flag)
                    {
                        int temp = Convert.ToInt32(str);
                        bool flag2 = temp > index;
                        if (flag2)
                        {
                            index = temp;
                            currentFile = file;
                        }
                    }
                    else
                    {
                        index = 1;
                        currentFile = file;
                    }
                }
            }
            bool flag3 = currentFile != null;
            if (flag3)
            {
                FileInfo fileInfo = new FileInfo(currentFile);
                size = fileInfo.Length;
            }
        }

        // Token: 0x04000563 RID: 1379
        private static string _path = null;

        // Token: 0x04000564 RID: 1380
        private static readonly int _fileSize = 10485760;

        // Token: 0x04000565 RID: 1381
        private static readonly Dictionary<string, int> _dictIndex = new Dictionary<string, int>();

        // Token: 0x04000566 RID: 1382
        private static readonly Dictionary<string, long> _dictSize = new Dictionary<string, long>();

        // Token: 0x04000567 RID: 1383
        private static readonly Dictionary<string, FileStream> _dictStream = new Dictionary<string, FileStream>();

        // Token: 0x04000568 RID: 1384
        private static readonly Dictionary<string, StreamWriter> _dictWriter = new Dictionary<string, StreamWriter>();
    }

  public class LogUtil
    {
        // Token: 0x06000B5C RID: 2908 RVA: 0x00035544 File Offset: 0x00033744
        private static string CreateLogString(string prefix, string log)
        {
            return string.Format("{0} {1} {2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), prefix.PadRight(7, ' '), log);
        }

        // Token: 0x06000B5D RID: 2909 RVA: 0x0003557C File Offset: 0x0003377C
        public static Task Debug(string log)
        {
            return Task.Factory.StartNew(delegate ()
            {
                object @lock = _lock;
                lock (@lock)
                {
                    log = CreateLogString("[Debug]", log);
                    string path = LogFileHelper.CreatePath("Log\\Debug\\", log);
                    LogFileHelper.WriteFile(log, path);
                }
            });
        }

        // Token: 0x06000B5E RID: 2910 RVA: 0x000355BC File Offset: 0x000337BC
        public static Task LogError(Exception ex)
        {
            return LogError(string.Concat(new string[]
            {
                DateTime.Now.ToString("yyyy/MM/dd HH/mm/ss"),
                ":",
                ex.Message,
                "\r\n",
                ex.StackTrace
            }));
        }

        // Token: 0x06000B5F RID: 2911 RVA: 0x00035624 File Offset: 0x00033824
        public static Task LogError(string log)
        {
            return Task.Factory.StartNew(delegate ()
            {
                object @lock = _lock;
                lock (@lock)
                {
                    log = CreateLogString("[Error]", log);
                    string path = LogFileHelper.CreatePath("Log\\Error\\", log);
                    LogFileHelper.WriteFile(log, path);
                }
            });
        }

        // Token: 0x06000B60 RID: 2912 RVA: 0x00035664 File Offset: 0x00033864
        public static Task Log(string log)
        {
            return Task.Factory.StartNew(delegate ()
            {
                object @lock = _lock;
                lock (@lock)
                {
                    log = CreateLogString("[Info]", log);
                    string path = LogFileHelper.CreatePath("Log\\Info\\", log);
                    LogFileHelper.WriteFile(log, path);
                }
            });
        }

        // Token: 0x04000569 RID: 1385
        private static readonly object _lock = new object();
    }

catch (Exception ex)
            {
                LogUtil.LogError(ex);
            }

还有LogUtil.Log(ex.Message);

LogUtil.Debug(ex.Message);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值