csharp C#超级简单记录器类//用法示例:https://heiswayi.nrird.com/2016/creating-super-simple-logger-class-in-csharp
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#超级简单记录器类//用法示例:https://heiswayi.nrird.com/2016/creating-super-simple-logger-class-in-csharp相关的知识,希望对你有一定的参考价值。
public class SimpleLogger
{
private readonly string datetimeFormat;
private readonly string logFilename;
/// <summary>
/// Initiate an instance of SimpleLogger class constructor.
/// If log file does not exist, it will be created automatically.
/// </summary>
public SimpleLogger()
{
datetimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
logFilename = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".log";
// Log file header line
string logHeader = logFilename + " is created.";
if (!System.IO.File.Exists(logFilename))
{
WriteLine(System.DateTime.Now.ToString(datetimeFormat) + " " + logHeader, false);
}
}
/// <summary>
/// Log a DEBUG message
/// </summary>
/// <param name="text">Message</param>
public void Debug(string text)
{
WriteFormattedLog(LogLevel.DEBUG, text);
}
/// <summary>
/// Log an ERROR message
/// </summary>
/// <param name="text">Message</param>
public void Error(string text)
{
WriteFormattedLog(LogLevel.ERROR, text);
}
/// <summary>
/// Log a FATAL ERROR message
/// </summary>
/// <param name="text">Message</param>
public void Fatal(string text)
{
WriteFormattedLog(LogLevel.FATAL, text);
}
/// <summary>
/// Log an INFO message
/// </summary>
/// <param name="text">Message</param>
public void Info(string text)
{
WriteFormattedLog(LogLevel.INFO, text);
}
/// <summary>
/// Log a TRACE message
/// </summary>
/// <param name="text">Message</param>
public void Trace(string text)
{
WriteFormattedLog(LogLevel.TRACE, text);
}
/// <summary>
/// Log a WARNING message
/// </summary>
/// <param name="text">Message</param>
public void Warning(string text)
{
WriteFormattedLog(LogLevel.WARNING, text);
}
private void WriteFormattedLog(LogLevel level, string text)
{
string pretext;
switch (level)
{
case LogLevel.TRACE:
pretext = System.DateTime.Now.ToString(datetimeFormat) + " [TRACE] ";
break;
case LogLevel.INFO:
pretext = System.DateTime.Now.ToString(datetimeFormat) + " [INFO] ";
break;
case LogLevel.DEBUG:
pretext = System.DateTime.Now.ToString(datetimeFormat) + " [DEBUG] ";
break;
case LogLevel.WARNING:
pretext = System.DateTime.Now.ToString(datetimeFormat) + " [WARNING] ";
break;
case LogLevel.ERROR:
pretext = System.DateTime.Now.ToString(datetimeFormat) + " [ERROR] ";
break;
case LogLevel.FATAL:
pretext = System.DateTime.Now.ToString(datetimeFormat) + " [FATAL] ";
break;
default:
pretext = "";
break;
}
WriteLine(pretext + text);
}
private void WriteLine(string text, bool append = true)
{
try
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilename, append, System.Text.Encoding.UTF8))
{
if (text != "")
{
writer.WriteLine(text);
}
}
}
catch
{
throw;
}
}
[System.Flags]
private enum LogLevel
{
TRACE,
INFO,
DEBUG,
WARNING,
ERROR,
FATAL
}
}
以上是关于csharp C#超级简单记录器类//用法示例:https://heiswayi.nrird.com/2016/creating-super-simple-logger-class-in-csharp的主要内容,如果未能解决你的问题,请参考以下文章
csharp 一个简单的C#示例,用于演示与MongoDB聚合结果交互的多态抽象类。
csharp 用C#编写的简单SMTP邮件客户端助手类,用于异步发送电子邮件。注意:使用Log4Net进行日志记录。
csharp 使用简单代理的示例服务类
csharp 使用递归实现解决方案以在迷宫中查找路径的示例。从C#h的计算机编程基础知识出发
csharp 使用递归实现解决方案以在迷宫中查找路径的示例。从C#h的计算机编程基础知识出发
csharp 如何在C#中定义和使用类和对象的基本示例。