-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRollingLogger.cs
87 lines (78 loc) · 2.93 KB
/
RollingLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Created by SharpDevelop.
* User: lextm
* Date: 3/14/2010
* Time: 1:18 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Lextm.SharpSnmpLib;
using Lextm.SharpSnmpLib.Messaging;
using Lextm.SharpSnmpLib.Pipeline;
namespace snmpd
{
/// <summary>
/// Logger class, who logs message processed to the rolling log file.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
internal class RollingLogger : ILogger
{
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private const string Empty = "-";
public RollingLogger()
{
if (!Logger.IsInfoEnabled)
{
return;
}
Logger.Info(string.Format(CultureInfo.InvariantCulture, "#Software: #SNMP Agent {0}", System.Reflection.Assembly.GetEntryAssembly().GetName().Version));
Logger.Info("#Version: 1.0");
Logger.Info(string.Format(CultureInfo.InvariantCulture, "#Date: {0}", DateTime.UtcNow));
Logger.Info("#Fields: date time s-ip cs-method cs-uri-stem s-port cs-username c-ip sc-status cs-version time-taken");
}
public void Log(ISnmpContext context)
{
if (Logger.IsInfoEnabled)
{
Logger.Info(GetLogEntry(context));
}
}
private static string GetLogEntry(ISnmpContext context)
{
return string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}",
DateTime.UtcNow,
context.Binding.Endpoint.Address,
context.Request.TypeCode() == SnmpType.Unknown ? Empty : context.Request.TypeCode().ToString(),
GetStem(context.Request.Pdu().Variables),
context.Binding.Endpoint.Port,
context.Request.Parameters.UserName,
context.Sender.Address,
(context.Response == null) ? Empty : context.Response.Pdu().ErrorStatus.ToErrorCode().ToString(),
context.Request.Version,
DateTime.Now.Subtract(context.CreatedTime).TotalMilliseconds);
}
private static string GetStem(ICollection<Variable> variables)
{
if (variables.Count == 0)
{
return Empty;
}
StringBuilder result = new StringBuilder();
foreach (Variable v in variables)
{
result.AppendFormat("{0};", v.Id);
}
if (result.Length > 0)
{
result.Length--;
}
return result.ToString();
}
}
}