/* ======================================================================== * Copyright (c) 2005-2026 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 System.Collections.Concurrent; using System.Collections.Generic; using Microsoft.Extensions.Logging; namespace Opc.Ua.Tests { /// /// Thread-safe logger provider for asserting structured log contracts. /// public sealed class RecordingLoggerProvider : ILoggerProvider { /// /// Initializes a recording provider. /// /// The minimum level to capture. public RecordingLoggerProvider(LogLevel minimumLevel = LogLevel.Trace) { MinimumLevel = minimumLevel; } /// /// Gets or sets the minimum captured log level. /// public LogLevel MinimumLevel { get; set; } /// /// Gets a snapshot of the captured records. /// public IReadOnlyList Records => m_records.ToArray(); /// public ILogger CreateLogger(string categoryName) { return new RecordingLogger(this, categoryName); } /// public void Dispose() { } private sealed class RecordingLogger : ILogger { public RecordingLogger(RecordingLoggerProvider provider, string categoryName) { m_provider = provider; m_categoryName = categoryName; } /// public IDisposable BeginScope(TState state) where TState : notnull { return EmptyScope.Instance; } /// public bool IsEnabled(LogLevel logLevel) { return logLevel >= m_provider.MinimumLevel && logLevel != LogLevel.None; } /// public void Log( LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { if (!IsEnabled(logLevel)) { return; } var properties = new Dictionary(StringComparer.Ordinal); if (state is IEnumerable> values) { foreach (KeyValuePair value in values) { properties[value.Key] = value.Value; } } m_provider.m_records.Enqueue( new RecordedLogRecord( m_categoryName, logLevel, eventId, formatter(state, exception), properties, exception)); } private readonly RecordingLoggerProvider m_provider; private readonly string m_categoryName; } private sealed class EmptyScope : IDisposable { public static EmptyScope Instance { get; } = new(); public void Dispose() { } } private readonly ConcurrentQueue m_records = new(); } /// /// A structured log record captured by . /// public sealed class RecordedLogRecord { /// /// Initializes a captured log record. /// public RecordedLogRecord( string categoryName, LogLevel logLevel, EventId eventId, string message, IReadOnlyDictionary properties, Exception? exception) { CategoryName = categoryName; LogLevel = logLevel; EventId = eventId; Message = message; Properties = properties; Exception = exception; } /// /// Gets the logger category. /// public string CategoryName { get; } /// /// Gets the log level. /// public LogLevel LogLevel { get; } /// /// Gets the event identity. /// public EventId EventId { get; } /// /// Gets the formatted message. /// public string Message { get; } /// /// Gets the structured log properties. /// public IReadOnlyDictionary Properties { get; } /// /// Gets the associated exception. /// public Exception? Exception { get; } } }