c#日志生成
2021/5/4 12:55:13
本文主要是介绍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"); } } public 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 } }
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"); } }
public 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 }}
这篇关于c#日志生成的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具