异常日志文件errorlong
#region log
////////////////////use///////////////
/// <summary>
/// 异常日志
/// </summary>
/// <param name="ex">Exception ex</param>
/// <param name="flagTypeRemark">异常类型备注</param>
public void LogException(Exception ex, string flagTypeRemark = "**")
{
AppPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"file\Error\";
log(AppPath, "\r\n##" + flagTypeRemark + "##\r\nMessage:" + ex.Message + "\r\nStacktrace:" + ex.StackTrace + "\r\n");
} /// <summary>
/// 本地日志记录
/// </summary>
/// <param name="logPath">日志路径</param>
/// <param name="logContent">要记录的日志内容</param>
public static void log(string logPath, string logContent)
{
string filePath = AppPath + "elog.log";
string content = DateTime.Now.ToString("yyyyMMddHHmmss:") + logContent;
if (!System.IO.Directory.Exists(AppPath)) System.IO.Directory.CreateDirectory(AppPath);
if (!System.IO.File.Exists(filePath))
{
System.IO.File.AppendAllText(filePath, content);
return;
}
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(writeLog);
Thread thread = new Thread(threadStart);
thread.Name = "Pro_ErrorLog.log";
thread.Start(logContent);
} /// <summary>
/// 当前程序运行路径
/// </summary>
public static string AppPath { get; set; } public static void writeLog(object str)
{
string filePath = AppPath + "elog.log";
string content = "\r\n" + DateTime.Now.ToString("yyyyMMddHHmmss:") + str.ToString();
System.IO.FileInfo info = new System.IO.FileInfo(filePath);
if (info.Length > * * )
{
while (IsFileInUse(filePath))
Thread.Sleep();
string backPath = AppPath + @"BackError\";
if (!System.IO.Directory.Exists(backPath)) System.IO.Directory.CreateDirectory(backPath);
System.IO.File.Move(filePath, backPath + "elog" + DateTime.Now.ToString("yyyyMMdd") + ".log");
System.IO.File.Delete(filePath);
}
while (IsFileInUse(filePath))
Thread.Sleep();
if (!IsFileInUse(filePath))
{
#region write file
System.IO.FileStream fs = null;
try
{
fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None);
fs.Write(Encoding.UTF8.GetBytes(content), , Encoding.UTF8.GetByteCount(content));
}
catch
{
;
}
finally
{
if (fs != null)
fs.Close();
}
#endregion
}
} /// <summary>
/// 文件是否被占用??
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool IsFileInUse(string fileName)
{
bool inUse = true;
System.IO.FileStream fs = null;
try
{
fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None);
inUse = false;
}
catch
{
inUse = true;
}
finally
{
if (fs != null)
fs.Close();
}
return inUse;
}
#endregion
//use
LogException(ex, "异常类型备注文本");//
异常日志文件errorlong的更多相关文章
- Oracle 监听器日志文件过大导致监听异常
Oracle 监听器日志文件过大导致监听异常 db版本:11.2.0.1 os版本:windows2008 现象: 应用异常,无法连接数据库.登陆数据库服务器,查看监听已经断掉.尝试重启监听,重启失败 ...
- log4j.properties配置与将异常输出到Log日志文件实例
将异常输出到 log日志文件 实际项目中的使用: <dependencies> <dependency> <groupId>org.slf4j</groupI ...
- C# 插件热插拔 .NET:何时应该 “包装异常”? log4.net 自定义日志文件名称
C# 插件热插拔 所谓热插拔就是插件可以 在主程序不重新启动的情况直接更新插件, 网上有很多方案: https://www.cnblogs.com/happyframework/p/3405811 ...
- Log4Net异常日志记录在asp.net mvc3.0的应用
前言 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是简单的介绍如何在Visual ...
- 记一次log4j日志文件输出错误的解决
log4j错误信息:log4j:ERROR Failed to rename [D:/logs/wmts_] to [D:/logs/wmts_2015-12-21.log ]. 起因:部门网站使用B ...
- Log4net按照不同级别写入多个日志文件
[assembly: log4net.Config.XmlConfigurator(Watch = true)]//注入 在一个Web应用项目中,我使用了Fluent NHibernate作为数据访问 ...
- Android中对Log日志文件的分析[转]
一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...
- 使用Log4Net完成异常日志处理
1.在MVC的Modal文件夹建一个异常处理过滤器 public class MyExceptionAttribute:HandleErrorAttribute { public static Que ...
- SQL Server 2008 收缩日志 清空删除大日志文件 转载
SQL Server 2008 收缩日志 清空删除大日志文件 由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005)Ba ...
随机推荐
- EFK教程(3) - ElasticSearch冷热数据分离
基于ElasticSearch多实例架构,实现资源合理分配.冷热数据分离 作者:"发颠的小狼",欢迎转载与投稿 目录 ▪ 用途 ▪ 架构 ▪ 192.168.1.51 elasti ...
- Selenium WebDriver 中鼠标事件
鼠标点击操作 鼠标点击事件有以下几种类型: 清单 1. 鼠标左键点击 Actions action = new Actions(driver);action.click();// 鼠标左键在当 ...
- 5分钟上手自动化测试——Airtest+Poco快速上手
版权声明:该文章为AirtestProject原创文章:允许转载,但转载必须注明“转载”并保留原链接 前言 本文档将演示使用`AirtestProject`专用的编辑器AirtestIDE,编写`Ai ...
- python排序算法之一:冒泡排序(及其优化)
相信冒泡排序已经被大家所熟知,今天看了一篇文章,大致是说在面试时end在了冒泡排序上,主要原因是不能给出冒泡排序的优化. 所以,今天就写一下python的冒泡排序算法,以及给出一个相应的优化.OK,前 ...
- 【JavaEE】之SSM-Maven依赖积累
本帖中收集JavaEE SSM框架使用的Maven版本库中的依赖.本帖持续更新...... 日志输出: <dependency> <groupId>commons-loggin ...
- 【Android - 自定义View】之View的位置参数
View是Android中所有控件的基类,不管是简单的Button和TextView,还是复杂的RelativeLayout和ListView,其基类都是View类:ViewGroup也继承了View ...
- 【Android - 控件】之MD - RecyclerView的使用
RecyclerView是Android 5.0新特性——Material Design中的一个控件,它将ListView.GridView整合到一起,可以使用极少的代码在ListView.GridV ...
- 以Python为例的Async / Await的编程基础
来源:Redislabs 作者:Loris Cro 翻译:Kevin (公众号:中间件小哥) 近年来,许多编程语言都在努力改进它们的并发原语.Go 语言有 goroutines,Ruby 有 fibe ...
- python_编程面试题
使用递归方法对一个数组求最大值和最小值 """ 用递归算法求解一个数组的最大值和最小值 思路: 1.首先假设这个列表只有1个元素或两个元素 2.再考虑超过两个元素的情况, ...
- 第五章 Unity中的基础光照(2)
目录 1. Unity中的环境光和自发光 2. 在UnityShader中实现漫反射光照模型 2.1 实践:逐顶点光照 2.2 实践:逐像素光照 2.3 半兰伯特模型 1. Unity中的环境光和自发 ...