forked from 47-studio-org/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoupeLogRecordBuilder.cs
73 lines (61 loc) · 2.18 KB
/
LoupeLogRecordBuilder.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
using System;
using System.Linq;
using Gibraltar.Agent;
using System.Text;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.RecordBuilders;
using PostSharp.Patterns.Formatters;
namespace PostSharp.Samples.Logging.Loupe
{
/// <summary>
/// <see cref="LogRecordBuilder"/> for <see cref="LoupeLoggingBackend"/>.
/// </summary>
[Log(AttributeExclude = true)]
public class LoupeLogRecordBuilder : TextLogRecordBuilder, IMessageSourceProvider
{
/// <summary>
/// Initializes a new <see cref="LoupeLogRecordBuilder"/>.
/// </summary>
/// <param name="backend">The parent <see cref="LoupeLoggingBackend"/>.</param>
public LoupeLogRecordBuilder(LoupeLoggingBackend backend) : base(backend)
{
}
string IMessageSourceProvider.MethodName => this.MemberName;
string IMessageSourceProvider.ClassName => this.TypeSource.FullName;
string IMessageSourceProvider.FileName => this.SourceLineInfo.File;
int IMessageSourceProvider.LineNumber => this.SourceLineInfo.Line;
/// <inheritdoc />
protected override void AppendSourceLineInfo()
{
// We don't append source line info to the text since Gibraltar supports it natively.
}
/// <inheritdoc />
protected override void Write(UnsafeString message)
{
Log.Write(TranslateLevel(this.Level), "PostSharp", this, null, this.Exception, LogWriteMode.Queued,
null, this.TypeSource.Role + "." + this.RecordKind.ToString(), message.ToString(), null);
}
private static LogMessageSeverity TranslateLevel(LogLevel level)
{
switch (level)
{
case LogLevel.None:
return LogMessageSeverity.None;
case LogLevel.Trace:
return LogMessageSeverity.Verbose;
case LogLevel.Debug:
return LogMessageSeverity.Verbose;
case LogLevel.Info:
return LogMessageSeverity.Information;
case LogLevel.Warning:
return LogMessageSeverity.Warning;
case LogLevel.Error:
return LogMessageSeverity.Error;
case LogLevel.Critical:
return LogMessageSeverity.Critical;
default:
return LogMessageSeverity.Information;
}
}
}
}