/* ======================================================================== * 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 { /// /// A sample serilog trace logger replacement. /// public class SerilogTraceLogger { private int m_traceMask; private ILogger m_logger; /// /// Create a serilog trace logger which replaces the default logging. /// /// The application configuration. /// The min log level for file output. public static SerilogTraceLogger Create( ApplicationConfiguration config, LogEventLevel fileMinimumLevel = LogEventLevel.Information) { return Create(null, config, fileMinimumLevel); } /// /// Create a serilog trace logger which replaces the default logging. /// /// The logger configuration. /// The application configuration. /// The min log level for file output. public static SerilogTraceLogger Create( LoggerConfiguration loggerConfiguration, ApplicationConfiguration config, LogEventLevel fileMinimumLevel = LogEventLevel.Information) { 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.Day, rollOnFileSizeLimit: true, restrictedToMinimumLevel: fileMinimumLevel, retainedFileCountLimit: 10, flushToDiskInterval: TimeSpan.FromSeconds(13)); } ILogger logger = loggerConfiguration .MinimumLevel.Information() .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; } /// /// Ctor of trace logger. /// /// The logger /// The trace mask public SerilogTraceLogger(ILogger logger, int traceMask) { m_logger = logger; m_traceMask = traceMask; } /// /// Callback for logging OPC UA stack trace output /// /// Sender object /// The trace event args. 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.OperationDetail: case Utils.TraceMasks.Information: m_logger.Verbose(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.Information(e.Format, e.Arguments); break; } } } } }