-
Notifications
You must be signed in to change notification settings - Fork 1k
/
LogEvent.cs
104 lines (91 loc) · 3.55 KB
/
LogEvent.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//-----------------------------------------------------------------------
// <copyright file="LogEvent.cs" company="Akka.NET Project">
// Copyright (C) 2009-2019 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2019 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Akka.Actor;
namespace Akka.Event
{
/// <summary>
/// INTERNAL API.
///
/// Avoids redundant parsing of log levels and other frequently-used log items
/// </summary>
internal static class LogFormats
{
public static readonly IReadOnlyDictionary<LogLevel, string> PrettyPrintedLogLevel;
static LogFormats()
{
var dict = new Dictionary<LogLevel, string>();
foreach(LogLevel i in Enum.GetValues(typeof(LogLevel)))
{
dict.Add(i, Enum.GetName(typeof(LogLevel), i).Replace("Level", "").ToUpperInvariant());
}
PrettyPrintedLogLevel = dict;
}
public static string PrettyNameFor(this LogLevel level)
{
return PrettyPrintedLogLevel[level];
}
}
/// <summary>
/// This class represents a logging event in the system.
/// </summary>
public abstract class LogEvent : INoSerializationVerificationNeeded
{
/// <summary>
/// Initializes a new instance of the <see cref="LogEvent" /> class.
/// </summary>
protected LogEvent()
{
Timestamp = DateTime.UtcNow;
Thread = Thread.CurrentThread;
}
/// <summary>
/// The exception that caused the log event. Can be <c>null</c>
/// </summary>
public Exception Cause { get; protected set; }
/// <summary>
/// The timestamp that this event occurred.
/// </summary>
public DateTime Timestamp { get; private set; }
/// <summary>
/// The thread where this event occurred.
/// </summary>
public Thread Thread { get; private set; }
/// <summary>
/// The source that generated this event.
/// </summary>
public string LogSource { get; protected set; }
/// <summary>
/// The type that generated this event.
/// </summary>
public Type LogClass { get; protected set; }
/// <summary>
/// The message associated with this event.
/// </summary>
public object Message { get; protected set; }
/// <summary>
/// Retrieves the <see cref="Akka.Event.LogLevel" /> used to classify this event.
/// </summary>
/// <returns>The <see cref="Akka.Event.LogLevel" /> used to classify this event.</returns>
public abstract LogLevel LogLevel();
/// <summary>
/// Returns a <see cref="System.String" /> that represents this LogEvent.
/// </summary>
/// <returns>A <see cref="System.String" /> that represents this LogEvent.</returns>
public override string ToString()
{
if(Cause == null)
return
$"[{LogLevel().PrettyNameFor()}][{Timestamp}][Thread {Thread.ManagedThreadId.ToString().PadLeft(4, '0')}][{LogSource}] {Message}";
return
$"[{LogLevel().PrettyNameFor()}][{Timestamp}][Thread {Thread.ManagedThreadId.ToString().PadLeft(4, '0')}][{LogSource}] {Message}{Environment.NewLine}Cause: {Cause}";
}
}
}