文本日志

文本日志,顾名思义,就是将系统内部的信息、警告、错误、调试等信息记录在服务器上的系统文本文件上的一种归档日志。

文本日志的要求:

  1、可配置文本日志的记录位置;

  2、具备分文件写日志的能力;

  3、具备在系统的一处配置,其它各层都能调用的能力;

  4、能够记录程序产生日志的地点、时间、参与者和具体信息;

 

在实际应用中,发现有些比较好的第三方控件比较好用,能满足记录文本日志的要求。

本人用的比较多的是NLog组件。但因为格式和使用问题,对它做了一点小的封装。

 

TextLogger.cs

View Code
 1 public class TextLogger
 2     {
 3         private static readonly Logger _log = LogManager.GetCurrentClassLogger();
 4         private readonly string _logger;
 5 
 6         public TextLogger(string logger)
 7         {
 8             _logger = logger;
 9         }
10         /// <summary>
11         ///     记录正常信息日志
12         /// </summary>
13         public void Info(string format,params object[] args) { WriteLog(LogLevel.Info,null,format,args); }
14         /// <summary>
15         ///     记录错误信息日志
16         /// </summary>
17         public void Error(string format, params object[] args) { WriteLog(LogLevel.Error, null, format, args); }
18         /// <summary>
19         ///     记录错误信息日志
20         /// </summary>
21         public void Error(Exception ex,string format, params object[] args) { WriteLog(LogLevel.Error, ex, format, args); }
22         /// <summary>
23         ///     记录警告信息日志
24         /// </summary>
25         public void Warn(string format, params object[] args) { WriteLog(LogLevel.Warn, null, format, args); }
26         /// <summary>
27         ///     记录警告信息日志
28         /// </summary>
29         public void Warn(Exception ex, string format, params object[] args) { WriteLog(LogLevel.Warn, ex, format, args); }
30         /// <summary>
31         ///     记录跟踪日志
32         /// </summary>
33         /// <param name="format"></param>
34         /// <param name="args"></param>
35         public void Trace(string format, params object[] args) { WriteLog(LogLevel.Trace, null, format, args); }
36         /// <summary>
37         ///     记录跟踪日志
38         /// </summary>
39         public void Trace(Exception ex, string format, params object[] args) { WriteLog(LogLevel.Trace, ex, format, args); }
40 
41         private void WriteLog(LogLevel level,Exception ex,string format,params object[] args)
42         {
43             string message = string.Format(format ?? string.Empty, args);
44             if (ex != null) message = string.Concat(message, "\r\n", ex.Message);
45             message = string.Concat(_logger, "\r\n", message);
46             message = string.Concat(message, "\r\n--------------------------------------------------------\r\n");
47             _log.Log(level,message);
48         }
49     }

 

TextLogManager.cs

View Code
 1 public static class TextLogManager
 2     {
 3         /// <summary>
 4         /// 创建日志记录器
 5         /// </summary>
 6         /// <param name="type">所在类类名</param>
 7         /// <returns>返回文本日志记录器</returns>
 8         public static TextLogger Create(Type type)
 9         {
10             return Create(type.FullName);
11         }
12 
13         /// <summary>
14         /// 创建日志记录器
15         /// </summary>
16         /// <param name="logger">表述日志发生位置</param>
17         /// <returns>返回文本日志记录器</returns>
18         public static TextLogger Create(string logger)
19         {
20             return new TextLogger(logger);
21         }
22     }

 

调用方法:

View Code
1 private readonly TextLogger _tracing = TextLogManager.Create(typeof(FinancialDao));
2 
3 
4 _tracing.Error(“”);

 

配置文件:

View Code
 1 <configuration>
 2   <configSections>
 3     <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
 4 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 5     <targets>
 6       <target name="file" xsi:type="File" fileName="D:/Logs/${date:format=yyyyMM}/log_${date:format=ddHH}.log" layout="[${date:format=yyyy-MM-dd HH\:mm\:ss}][${level}] ${message} ${exception}"/>
 7     </targets>
 8     <rules>
 9       <logger name="*" minlevel="Debug" writeTo="file"/>
10     </rules>
11   </nlog>
12  </configSections>
13 </configuration>

 

转载于:https://www.cnblogs.com/nimin961145/archive/2013/03/18/2965826.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Zabbix的自定义监控功能,通过编写自定义脚本实现对文本日志进行监控,并在每次出现错误信息时发送告警。以下是一个实现的示例: 1. 编写自定义脚本 您可以使用脚本语言(如Python、Perl等)编写自定义脚本,用于监控文本日志文件,并在出现错误信息时发送告警。在脚本中,您可以使用Zabbix Sender API将告警信息发送到Zabbix Server。以下是一个Python脚本的示例: ```python #!/usr/bin/env python import os import sys import re import subprocess # 定义需要监控的日志文件路径 logfile = "/path/to/your/logfile" # 定义需要监控的关键字 keywords = ["error", "failed"] # 检查日志文件是否存在 if not os.path.exists(logfile): print("Log file does not exist") sys.exit(1) # 打开日志文件,并逐行读取 with open(logfile) as f: for line in f: # 判断当前行是否包含关键字 if any(keyword in line for keyword in keywords): # 发送告警信息到Zabbix Server subprocess.call(["zabbix_sender", "-z", "zabbix_server_ip", "-s", "hostname", "-k", "custom.key", "-o", "error message"]) ``` 在上述代码中,您需要将`/path/to/your/logfile`替换为您需要监控的日志文件路径,将`["error", "failed"]`替换为您需要监控的关键字列表,将`zabbix_server_ip`替换为您的Zabbix Server IP地址,将`hostname`替换为您的主机名,将`custom.key`替换为您的自定义键名,将`error message`替换为您需要发送的告警信息。 2. 创建Zabbix自定义监控项 在Zabbix Web界面中,创建一个新的自定义监控项,并将类型设置为`Zabbix trapper`,键名设置为上述脚本中的`custom.key`,主机设置为您的主机名。 3. 创建Zabbix触发器 在Zabbix Web界面中,创建一个新的触发器,将条件设置为上述自定义监控项的值等于1,并将动作设置为发送告警通知。 这样,当您的脚本监测到日志中出现错误信息时,将会发送告警信息到Zabbix Server,触发自定义监控项的值为1,从而触发告警通知。由于每个错误日志都会触发自定义监控项的值为1,因此您将会收到每个错误日志的告警通知。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值