时间:2021-05-25
复制代码 代码如下:
using System;
using System.IO;
using System.Text;
public class LogInfo
{
private string ErrorInfo_User = ""; // 记录用户自定义错误信息
private string ErrorPosition = ""; // 记录错误的位置信息,可包括类、函数等
private string ErrorInfo_Sys = ""; // 记录系统产生的异常错误信息
// 记录日志信息
public void RecordErrorInfo(string Position, string Error_Sys, string Error_User)
{
ErrorPosition = Position;
ErrorInfo_Sys = Error_Sys;
ErrorInfo_User = Error_User;
}
// 自定义日志信息格式
private string GetLogContent()
{
string LogContent = "\r\n--------------------------------------------------------------------------\r\n";
LogContent += "[自定义异常日志][" + DateTime.Now.ToString() + "]\r\n";
LogContent += "=>[Position]=>[" + ErrorPosition + "]\r\n";
LogContent += "=>[UserInfo]=>[" + ErrorInfo_User + "]\r\n";
LogContent += "=>[SysInfo]=>[" + ErrorInfo_Sys + "]\r\n";
LogContent += "--------------------------------------------------------------------------\r\n";
return LogContent;
}
// 保存日志信息到文件
public void SaveLogToFile()
{
try
{
// get the file path of the log.
string FileName = @"log/LogInfo_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
// get the content of the log.
string LogContent = GetLogContent();
// create the stream of the log file and save the log.
FileStream smLog = new FileStream(FileName, FileMode.Append, FileAccess.Write);
// if the stream is correct.
if (smLog != null)
{
long lFileContentLen = smLog.Length;
smLog.Lock(0, lFileContentLen);
byte[] buffer = Encoding.GetEncoding("gb2312").GetBytes(LogContent);
smLog.Write(buffer, 0, buffer.Length);
smLog.Unlock(0, lFileContentLen);
smLog.Flush();
smLog.Close();
}
}
catch
{ }
}
}
public class LogExample
{
private LogInfo _Log = new LogInfo();
// 某处理函数一
public void Function_First()
{
try
{
// do something which could be occur exception.
}
catch (Exception error)
{
_Log.RecordErrorInfo("函数Function_First", error.Message.ToString(), "执行函数Function_First时,发生异常!");
_Log.SaveLogToFile();
}
}
// 某处理函数二
public void Function_Second()
{
try
{
// do something which could be occur exception.
}
catch (Exception error)
{
_Log.RecordErrorInfo("函数Function_Second", error.Message.ToString(), "执行函数Function_Second时,发生异常!");
_Log.SaveLogToFile();
}
}
}
class Program
{
static void Main(string[] args)
{
// 创建内建了日志的对象
LogExample Le = new LogExample();
// 执行处理操作一
Le.Function_First();
// 执行处理操作二
Le.Function_Second();
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
微信小程序自定义创建,最近自己捣鼓微信小程序的东西,这里对自定义创建做一个简单的资料整理,也许可以帮助大家。微信小程序自定义创建自定义创建与默认创建完全相同,只
下面将以权限的控制为例自定义一个标签:一、标签类型复制代码代码如下:步骤:1.自定义一个类PerssionTag继承SimpleTagSupport(自定义标签
php自定义错误日志项目中需要对定义错误日志及时处理,那么就需要修改自定义错误日志的输出方式(写日志、发邮件、发短信)一.register_shutdown_f
有时需要一些控件加上自定义的属性,下面就介绍自定义控件的简单实现1、新建一个vb的窗口程序2、在新建的项目上右键->添加->新建项3、向下拖动,找到自定义控件,
前言其实所谓自动分割MongoDB日志文件,就是指RotateMongoDBlogfiles,即让MongoDB每天(或每个星期,可自定义控制)生成一个日志文件