首页 技术 正文
技术 2022年11月9日
0 收藏 833 点赞 2,471 浏览 5860 个字
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;namespace app.Lib
{
public enum Level
{
Debug,
Exception,
Info,
Warn,
} public enum Priority
{
None,
High,
Medium,
Low,
}
public class Logger
{
private static StreamWriter _writer;
//private static string _format = "{1}: {2}. Priority: {3}. Timestamp:{0:u}";
private static string _format = "{0:u}:{1:D3} {2}: {3}";
private static StreamWriter Writer
{
get
{
if (_writer == null)
{
string path = Application.StartupPath + "//Log//" + DateTime.Now.ToString("yyyy") + "//" +
DateTime.Now.ToString("yyyyMM") + "//" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log";
string filepath = Application.StartupPath + "//Log//" + "//bmp//"; string dir = Path.GetDirectoryName(path);
string filedir = Path.GetDirectoryName(filepath); if (filedir != null && !Directory.Exists(filedir))
{
Directory.CreateDirectory(filedir);
} if (dir != null && !Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
_writer = new StreamWriter(path, true, Encoding.Default);
_writer.AutoFlush = true;
}
return _writer;
}
} public static string GetFileDirName()
{
return Application.StartupPath + "//Log" + "//bmp//";
} public static void Log(string message, Level level)
{
string messageToLog = String.Format(CultureInfo.InvariantCulture, _format, DateTime.Now, DateTime.Now.Millisecond,
level.ToString().ToUpper(CultureInfo.InvariantCulture), message); Writer.WriteLine(messageToLog);
} public static bool EnableLog;
}
}

很多时候需要为我们应用程序添加日子文件,方便问题定位

代码转载而来,非原创

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection; namespace CommonLib
{
/// <summary>
/// 日志类型
/// </summary>
public enum LogType
{
All,
Information,
Debug,
Success,
Failure,
Warning,
Error
} public class Logger
{ #region Instance
private static object logLock; private static Logger _instance; private static string logFileName;
private Logger() { } /// <summary>
/// Logger instance
/// </summary>
public static Logger Instance
{
get
{
if (_instance == null)
{
_instance = new Logger();
logLock = new object();
//logFileName = Guid.NewGuid() + ".log";
logFileName = DateTime.Now.Hour.ToString("") + DateTime.Now.Minute.ToString("") +DateTime.Now.Second.ToString("") + ".log";
}
return _instance;
}
}
#endregion /// <summary>
/// Write log to log file
/// </summary>
/// <param name="logContent">Log content</param>
/// <param name="logType">Log type</param>
public void WriteLog(string logContent, LogType logType = LogType.Information, string fileName = null)
{
try
{
string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
basePath = Directory.GetCurrentDirectory(); //@"C:\APILogs";
if (!Directory.Exists(basePath + "\\Log"))
{
Directory.CreateDirectory(basePath + "\\Log");
} string dataString = DateTime.Now.ToString("yyyy-MM-dd");
if (!Directory.Exists(basePath + "\\Log\\" + dataString))
{
Directory.CreateDirectory(basePath + "\\Log\\" + dataString);
} string[] logText = new string[] { DateTime.Now.ToString("hh:mm:ss") + ": " + logType.ToString() + ": " + logContent };
if (!string.IsNullOrEmpty(fileName))
{
fileName = fileName + "_" + logFileName;
}
else
{
fileName = logFileName;
//fileName = DateTime.Now.ToString("hh:mm:ss");
} lock (logLock)
{
File.AppendAllLines(basePath + "\\Log\\" + dataString + "\\" + fileName, logText);
}
}
catch (Exception) { }
} /// <summary>
/// Write exception to log file
/// </summary>
/// <param name="exception">Exception</param>
public void WriteException(Exception exception, string specialText = null)
{
if (exception != null)
{
Type exceptionType = exception.GetType();
string text = string.Empty;
if (!string.IsNullOrEmpty(specialText))
{
text = text + specialText + Environment.NewLine;
}
text = "Exception: " + exceptionType.Name + Environment.NewLine;
text += " " + "Message: " + exception.Message + Environment.NewLine;
text += " " + "Source: " + exception.Source + Environment.NewLine;
text += " " + "StackTrace: " + exception.StackTrace + Environment.NewLine;
WriteLog(text, LogType.Error);
}
} }
}
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection; namespace CommonLib
{
/// <summary>
/// 日志类型
/// </summary>
public enum LogType
{
All,
Information,
Debug,
Success,
Failure,
Warning,
Error
} public class Logger
{ #region Instance
private static object logLock; private static Logger _instance; private static string logFileName;
private Logger() { } /// <summary>
/// Logger instance
/// </summary>
public static Logger Instance
{
get
{
if (_instance == null)
{
_instance = new Logger();
logLock = new object();
//logFileName = Guid.NewGuid() + ".log";
logFileName = DateTime.Now.Hour.ToString("") + DateTime.Now.Minute.ToString("") +DateTime.Now.Second.ToString("") + ".log";
}
return _instance;
}
}
#endregion /// <summary>
/// Write log to log file
/// </summary>
/// <param name="logContent">Log content</param>
/// <param name="logType">Log type</param>
public void WriteLog(string logContent, LogType logType = LogType.Information, string fileName = null)
{
try
{
string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
basePath = Directory.GetCurrentDirectory(); //@"C:\APILogs";
if (!Directory.Exists(basePath + "\\Log"))
{
Directory.CreateDirectory(basePath + "\\Log");
} string dataString = DateTime.Now.ToString("yyyy-MM-dd");
if (!Directory.Exists(basePath + "\\Log\\" + dataString))
{
Directory.CreateDirectory(basePath + "\\Log\\" + dataString);
} string[] logText = new string[] { DateTime.Now.ToString("hh:mm:ss") + ": " + logType.ToString() + ": " + logContent };
if (!string.IsNullOrEmpty(fileName))
{
fileName = fileName + "_" + logFileName;
}
else
{
fileName = logFileName;
//fileName = DateTime.Now.ToString("hh:mm:ss");
} lock (logLock)
{
File.AppendAllLines(basePath + "\\Log\\" + dataString + "\\" + fileName, logText);
}
}
catch (Exception) { }
} /// <summary>
/// Write exception to log file
/// </summary>
/// <param name="exception">Exception</param>
public void WriteException(Exception exception, string specialText = null)
{
if (exception != null)
{
Type exceptionType = exception.GetType();
string text = string.Empty;
if (!string.IsNullOrEmpty(specialText))
{
text = text + specialText + Environment.NewLine;
}
text = "Exception: " + exceptionType.Name + Environment.NewLine;
text += " " + "Message: " + exception.Message + Environment.NewLine;
text += " " + "Source: " + exception.Source + Environment.NewLine;
text += " " + "StackTrace: " + exception.StackTrace + Environment.NewLine;
WriteLog(text, LogType.Error);
}
} }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,702
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741