forked from OPCFoundation/UA-.NETStandard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerilogTraceLogger.cs
More file actions
135 lines (125 loc) · 5.48 KB
/
SerilogTraceLogger.cs
File metadata and controls
135 lines (125 loc) · 5.48 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ========================================================================
* Copyright (c) 2005-2021 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using Opc.Ua;
using Serilog;
using Serilog.Events;
namespace Quickstarts.ReferenceServer
{
/// <summary>
/// A sample serilog trace logger replacement.
/// </summary>
public class SerilogTraceLogger
{
private int m_traceMask;
private ILogger m_logger;
/// <summary>
/// Create a serilog trace logger which replaces the default logging.
/// </summary>
/// <param name="config">The application configuration.</param>
/// <param name="fileMinimumLevel">The min log level for file output.</param>
public static SerilogTraceLogger Create(
ApplicationConfiguration config,
LogEventLevel fileMinimumLevel = LogEventLevel.Information)
{
return Create(null, config, fileMinimumLevel);
}
/// <summary>
/// Create a serilog trace logger which replaces the default logging.
/// </summary>
/// <param name="loggerConfiguration">The logger configuration.</param>
/// <param name="config">The application configuration.</param>
/// <param name="fileMinimumLevel">The min log level for file output.</param>
public static SerilogTraceLogger Create(
LoggerConfiguration loggerConfiguration,
ApplicationConfiguration config,
LogEventLevel fileMinimumLevel = LogEventLevel.Verbose)
{
if (loggerConfiguration == null)
{
loggerConfiguration = new LoggerConfiguration();
}
// add file logging
if (!string.IsNullOrWhiteSpace(config.TraceConfiguration.OutputFilePath))
{
loggerConfiguration.WriteTo.File(
Utils.ReplaceSpecialFolderNames(config.TraceConfiguration.OutputFilePath),
rollingInterval: RollingInterval.Infinite,
rollOnFileSizeLimit: true,
restrictedToMinimumLevel: fileMinimumLevel,
retainedFileCountLimit: 10,
flushToDiskInterval: TimeSpan.FromSeconds(13));
}
ILogger logger = loggerConfiguration
.MinimumLevel.Verbose()
.CreateLogger();
SerilogTraceLogger traceLogger = new SerilogTraceLogger(logger, config.TraceConfiguration.TraceMasks);
// disable the built in tracing, use serilog
Utils.SetTraceMask(Utils.TraceMask & Utils.TraceMasks.StackTrace);
Utils.SetTraceOutput(Utils.TraceOutput.Off);
Utils.Tracing.TraceEventHandler += traceLogger.TraceEventHandler;
return traceLogger;
}
/// <summary>
/// Ctor of trace logger.
/// </summary>
/// <param name="logger">The logger</param>
/// <param name="traceMask">The trace mask</param>
public SerilogTraceLogger(ILogger logger, int traceMask)
{
m_logger = logger;
m_traceMask = traceMask;
}
/// <summary>
/// Callback for logging OPC UA stack trace output
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">The trace event args.</param>
public void TraceEventHandler(object sender, TraceEventArgs e)
{
if ((e.TraceMask & m_traceMask) != 0)
{
if (e.Exception != null)
{
m_logger.Error(e.Exception, e.Format, e.Arguments);
return;
}
switch (e.TraceMask)
{
case Utils.TraceMasks.StartStop:
case Utils.TraceMasks.Information: m_logger.Information(e.Format, e.Arguments); break;
case Utils.TraceMasks.Error: m_logger.Error(e.Format, e.Arguments); break;
case Utils.TraceMasks.StackTrace:
case Utils.TraceMasks.Security: m_logger.Warning(e.Format, e.Arguments); break;
default: m_logger.Verbose(e.Format, e.Arguments); break;
}
}
}
}
}