|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015-2018 Rasmus Mikkelsen |
| 4 | +// Copyright (c) 2015-2018 eBay Software Foundation |
| 5 | +// https://github.com/eventflow/EventFlow |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 | +// this software and associated documentation files (the "Software"), to deal in |
| 9 | +// the Software without restriction, including without limitation the rights to |
| 10 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 11 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 12 | +// subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 19 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 20 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 21 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +using System; |
| 25 | +using System.Collections.Generic; |
| 26 | +using System.Linq; |
| 27 | +using EventFlow.Extensions; |
| 28 | +using EventFlow.TestHelpers; |
| 29 | +using FluentAssertions; |
| 30 | +using NUnit.Framework; |
| 31 | +using Serilog; |
| 32 | +using Serilog.Core; |
| 33 | +using Serilog.Events; |
| 34 | +using ILog = EventFlow.Logs.ILog; |
| 35 | +using LogLevel = EventFlow.Logs.LogLevel; |
| 36 | + |
| 37 | +namespace EventFlow.Tests.IntegrationTests.Logs |
| 38 | +{ |
| 39 | + public class LibLogTests : Test |
| 40 | + { |
| 41 | + [Test] |
| 42 | + public void SerilogTest() |
| 43 | + { |
| 44 | + var messages = new Dictionary<LogEventLevel, List<string>> |
| 45 | + { |
| 46 | + {LogEventLevel.Verbose, new List<string>()}, |
| 47 | + {LogEventLevel.Debug, new List<string>()}, |
| 48 | + {LogEventLevel.Information, new List<string>()}, |
| 49 | + {LogEventLevel.Warning, new List<string>()}, |
| 50 | + {LogEventLevel.Error, new List<string>()}, |
| 51 | + {LogEventLevel.Fatal, new List<string>()} |
| 52 | + }; |
| 53 | + |
| 54 | + Serilog.Log.Logger = new LoggerConfiguration() |
| 55 | + .MinimumLevel.Verbose() |
| 56 | + .WriteTo.Sink(new DummySink(messages)) |
| 57 | + .CreateLogger(); |
| 58 | + |
| 59 | + using (var resolver = EventFlowOptions.New |
| 60 | + .UseLibLog(LibLogProviders.Serilog) |
| 61 | + .CreateResolver()) |
| 62 | + { |
| 63 | + var log = resolver.Resolve<ILog>(); |
| 64 | + void TestLog(LogLevel logLevel, LogEventLevel logEventLevel) |
| 65 | + { |
| 66 | + var message = Guid.NewGuid().ToString("N"); |
| 67 | + log.Write(logLevel, message); |
| 68 | + messages[logEventLevel].FirstOrDefault(m => m == message).Should().NotBeNullOrEmpty(); |
| 69 | + } |
| 70 | + |
| 71 | + TestLog(LogLevel.Verbose, LogEventLevel.Verbose); |
| 72 | + TestLog(LogLevel.Debug, LogEventLevel.Debug); |
| 73 | + TestLog(LogLevel.Information, LogEventLevel.Information); |
| 74 | + TestLog(LogLevel.Warning, LogEventLevel.Warning); |
| 75 | + TestLog(LogLevel.Error, LogEventLevel.Error); |
| 76 | + TestLog(LogLevel.Fatal, LogEventLevel.Fatal); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private class DummySink : ILogEventSink |
| 81 | + { |
| 82 | + private readonly Dictionary<LogEventLevel, List<string>> _messages; |
| 83 | + |
| 84 | + public DummySink(Dictionary<LogEventLevel, List<string>> messages) |
| 85 | + { |
| 86 | + _messages = messages; |
| 87 | + } |
| 88 | + |
| 89 | + public void Emit(LogEvent logEvent) |
| 90 | + { |
| 91 | + _messages[logEvent.Level].Add(logEvent.RenderMessage()); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments