C#实现将程序运行信息写入日志的方法
作者:北风其凉 时间:2021-11-22 22:30:09
本文实例讲述了C#实现将程序运行信息写入日志的方法。分享给大家供大家参考。具体如下:
1.LogManager类
class LogManager
{
static object locker = new object();
/// <summary>
/// 重要信息写入日志
/// </summary>
/// <param name="logs">日志列表,每条日志占一行</param>
public static void WriteProgramLog(params string[] logs)
{
lock (locker)
{
string LogAddress = Environment.CurrentDirectory + "\\Log";
if (!Directory.Exists(LogAddress + "\\PRG"))
{
Directory.CreateDirectory(LogAddress + "\\PRG");
}
LogAddress = string.Concat(LogAddress, "\\PRG\\",
DateTime.Now.Year, '-', DateTime.Now.Month, '-',
DateTime.Now.Day, "_program.log");
StreamWriter sw = new StreamWriter(LogAddress, true);
foreach (string log in logs)
{
sw.WriteLine(string.Format("[{0}] {1}", DateTime.Now.ToString(), log));
}
sw.Close();
}
}
}
2.调用方法
LogManager.WriteProgramLog("日志1", "日志2", "日志3");
3.运行结果
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,日志
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
javaweb中Filter(过滤器)的常见应用
2023-07-10 23:57:36
java Map接口子类HashMap遍历与LinkedHashMap详解
2021-11-18 04:43:38
![](https://img.aspxhome.com/file/2023/1/78341_0s.jpg)
C# Winform 调用系统接口操作 INI 配置文件的代码
2023-03-04 11:49:54
SpringBoot整合Spring Data Elasticsearch的过程详解
2023-01-25 22:39:41
![](https://img.aspxhome.com/file/2023/7/80277_0s.png)
简单的java图片处理类(图片水印 图片缩放)
2022-04-02 03:24:37
Maven默认中央仓库(settings.xml 配置详解)
2021-10-17 12:25:40
C#多线程开发实战记录之线程基础
2022-11-03 03:21:45
![](https://img.aspxhome.com/file/2023/2/86962_0s.png)
c#获取数组中最大数的值
2022-07-20 07:49:02
从内存地址解析Java的static关键字的作用
2022-03-18 03:49:26
![](https://img.aspxhome.com/file/2023/2/86832_0s.png)
C#调用FFmpeg操作音视频的实现示例
2021-10-08 12:40:00
SpringBoot项目实战之加载和读取资源文件
2023-10-07 06:00:41
SpringBoot在RequestBody中使用枚举参数案例详解
2022-12-15 05:16:30
C# 操作Excel代码总结
2022-10-11 01:54:29
详解Android开发中Fragment的使用
2023-05-02 20:32:25
![](https://img.aspxhome.com/file/2023/7/68027_0s.png)
@SpringBootTest 注解报红问题及解决
2022-08-14 23:24:51
![](https://img.aspxhome.com/file/2023/6/71016_0s.jpg)
浅谈Java中的重载,重写,多态,静态绑定、动态绑定
2023-10-10 02:40:05
SpringIOC容器Bean的作用域及生命周期实例
2023-10-01 11:21:34
![](https://img.aspxhome.com/file/2023/1/76611_0s.png)
Springboot分页插件使用实例解析
2023-03-29 21:29:57
IDEA GIT 忽略文件的最佳方式推荐
2023-04-06 10:02:54
![](https://img.aspxhome.com/file/2023/6/63436_0s.jpg)
java文字转语音播报功能的实现方法
2022-05-08 18:44:41