#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的更多相关文章

  1. Oracle 监听器日志文件过大导致监听异常

    Oracle 监听器日志文件过大导致监听异常 db版本:11.2.0.1 os版本:windows2008 现象: 应用异常,无法连接数据库.登陆数据库服务器,查看监听已经断掉.尝试重启监听,重启失败 ...

  2. log4j.properties配置与将异常输出到Log日志文件实例

    将异常输出到 log日志文件 实际项目中的使用: <dependencies> <dependency> <groupId>org.slf4j</groupI ...

  3. C# 插件热插拔 .NET:何时应该 “包装异常”? log4.net 自定义日志文件名称

    C# 插件热插拔   所谓热插拔就是插件可以 在主程序不重新启动的情况直接更新插件, 网上有很多方案: https://www.cnblogs.com/happyframework/p/3405811 ...

  4. Log4Net异常日志记录在asp.net mvc3.0的应用

    前言 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是简单的介绍如何在Visual ...

  5. 记一次log4j日志文件输出错误的解决

    log4j错误信息:log4j:ERROR Failed to rename [D:/logs/wmts_] to [D:/logs/wmts_2015-12-21.log ]. 起因:部门网站使用B ...

  6. Log4net按照不同级别写入多个日志文件

    [assembly: log4net.Config.XmlConfigurator(Watch = true)]//注入 在一个Web应用项目中,我使用了Fluent NHibernate作为数据访问 ...

  7. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...

  8. 使用Log4Net完成异常日志处理

    1.在MVC的Modal文件夹建一个异常处理过滤器 public class MyExceptionAttribute:HandleErrorAttribute { public static Que ...

  9. SQL Server 2008 收缩日志 清空删除大日志文件 转载

    SQL Server 2008 收缩日志 清空删除大日志文件 由于SQL2008对文件和日志管理进行了优化,所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消:(SQL2005)Ba ...

随机推荐

  1. ubuntu 16.04 和 windows 10系统安装mysql 允许远程访问 | mysql user guide on ubuntu 16.04 and windows 10

    本文首发于个人博客https://kezunlin.me/post/36e618e7/,欢迎阅读! mysql user guide on ubuntu 16.04 and windows 10 Pa ...

  2. windows下搭建dubbo 环境(dubbo-admin和服务提供者消费者)

    ---恢复内容开始--- 一.  dubbo-admin管理控制台 从 https://github.com/apache/dubbo-admin clone项目到本地. 修改dubbo-admin- ...

  3. php如何处理大数据高并发

    大数据解决方案 使用缓存: 使用方式:1,使用程序直接保存到内存中.主要使用Map,尤其ConcurrentHashMap. 使用缓存框架.常用的框架:Ehcache,Memcache,Redis等. ...

  4. 【2018寒假集训 Day2】【动态规划】维修栅栏

    维修栅栏 问题描述: 小z最近当上了农场主!不过,还没有来得及庆祝,一件棘手的问题就摆在了小z的面前.农场的栅栏,由于年久失修,出现了多处破损.栅栏是由n块木板组成的,每块木板可能已经损坏也可能没有损 ...

  5. [ch03-02] 交叉熵损失函数

    系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 3.2 交叉熵损失函数 交叉熵(Cross Entrop ...

  6. Spring源码分析之AOP

    1.AOP简介 AOP即面向切面编程(Aspect Oriented Programming),通过预编译方式及运行期动态代理实现程序功能的统一维护的一种技术.使用aop对业务逻辑的各个部分进行隔离, ...

  7. Node_exporter一键安装部署脚本(Shell)

    #!/bin/bash # # rhel7. 安装node_exporter 用于监控数据采集 # Usage: # sh addNode.sh #Logs: /var/log/messages #H ...

  8. 为什么 main 方法是 public static void ?

    Main方法是我们学习Java编程语言时知道的第一个方法,你是否曾经想过为什么main方法是public.static.void的.当然,很多人首先学的是C和C++,但是在Java中main方法与前者 ...

  9. CSS浮动和各种定位

    CSS定位 css定位机制 文档流:元素按照在HTML中的位置决定排布的过程 块级元素是从上到下的,内联元素是从左到右的 浮动 position布局 position css position属性用于 ...

  10. Hyperledger Fabric手动生成CA证书搭建Fabric网络

    之前介绍了使用官方脚本自动化启动一个Fabric网络,并且所有的证书都是通过官方的命令行工具cryptogen直接生成网络中的所有节点的证书.在开发环境可以这么简单进行,但是生成环境下还是需要我们自定 ...