c#日志生成
标签:static path 运行 share close bug generic coding enc
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
///
/// 打印error类
///
public class LogUtil
{
private string path = string.Empty;
private static Dictionarylong, long> lockDic = new Dictionarylong, 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();
}
}
///
/// 日志写入
///
/// 要写入的字符串
/// 是否是文本追加
public void Error(string str, bool isAppend = true)
{
Write(path + "/Error/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str);
}
///
/// 日志写入
///
/// 要写入的字符串
/// 是否是文本追加
public void Debug(string str, bool isAppend = true)
{
Write(path + "/Debug/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str);
}
///
/// 日志写入
///
/// 要写入的字符串
/// 是否是文本追加
public void Info(string str, bool isAppend = true)
{
Write(path + "/Info/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str);
}
///
/// 程序运行环境
///
public enum Enviroment
{
///
/// webapi环境
///
HTTP,
///
/// 客户端
///
CLIENT
}
}
using System;using System.Collections.Generic;using System.IO;using System.Text;
/// /// 打印error类/// public class LogUtil{ private string path = string.Empty; private static Dictionary lockDic = new Dictionary(); 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(); } }
/// /// 日志写入 /// /// 要写入的字符串 /// 是否是文本追加 public void Error(string str, bool isAppend = true) { Write(path + "/Error/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str); }
/// /// 日志写入 /// /// 要写入的字符串 /// 是否是文本追加 public void Debug(string str, bool isAppend = true) {
Write(path + "/Debug/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str); }
/// /// 日志写入 /// /// 要写入的字符串 /// 是否是文本追加 public void Info(string str, bool isAppend = true) { Write(path + "/Info/" + DateTime.Now.ToString("yyyyMMdd") + ".txt", DateTime.Now.ToString() + "---------" + str); }
/// /// 程序运行环境 /// public enum Enviroment { /// /// webapi环境 /// HTTP, /// /// 客户端 /// CLIENT }}
c#日志生成
标签:static path 运行 share close bug generic coding enc
原文地址:https://www.cnblogs.com/Transmuter/p/14729241.html
文章来自:
搜素材网的
编程语言模块,转载请注明文章出处。
文章标题:
c#日志生成
文章链接:http://soscw.com/index.php/essay/88627.html
评论