public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_events_from_multiple_threads_should_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl, Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample trace message."; Action action1 = () => { Logger.Trace(expectedMessage); Wait.For(1).Seconds(); }; Action action2 = () => { Logger.Error(expectedMessage); Wait.For(1).Seconds(); }; Parallel.Invoke(action1, action2); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Trace" && x.Message == expectedMessage); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Error" && x.Message == expectedMessage); }
public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_events_after_client_disconnection_should_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl + "Logging", Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample trace message."; Logger.Info(expectedMessage); LogManager.Flush(); target.Proxy.Stop(); Logger.Error(expectedMessage); LogManager.Shutdown(); Assert.True(LoggingHub.LogEvents.TryDequeue(out var logEvent)); Assert.AreEqual("Info", logEvent.Level); Assert.AreEqual(expectedMessage, logEvent.Message); Assert.True(LoggingHub.LogEvents.TryDequeue(out logEvent)); Assert.AreEqual("Error", logEvent.Level); Assert.AreEqual(expectedMessage, logEvent.Message); }
public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_events_after_server_disconnects_and_reconnects_should_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl, Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample message."; Logger.Trace(expectedMessage); Wait.For(1).Seconds(); StopHub(); StartHub(); Logger.Error(expectedMessage); Wait.Until(() => Test.Current.SignalRLogEvents.Count == 2); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Trace" && x.Message == expectedMessage); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Error" && x.Message == expectedMessage); }
public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_events_after_client_disconnection_should_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl, Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample trace message."; Logger.Trace(expectedMessage); Wait.For(1).Seconds(); target.Proxy.Stop(); Logger.Error(expectedMessage); Wait.For(1).Seconds(); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Trace" && x.Message == expectedMessage); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Error" && x.Message == expectedMessage); }
public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_events_after_server_disconnects_and_reconnects_should_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl, Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample message."; Logger.Trace(expectedMessage); Wait.Until(() => Test.Current.SignalRLogEvents.FirstOrDefault(x => x.Level == "Trace" && x.Message == expectedMessage) != null, Timeout); StopHub(); StartHub(); Logger.Error(expectedMessage); Wait.Until(() => Test.Current.SignalRLogEvents.FirstOrDefault(x => x.Level == "Error" && x.Message == expectedMessage) != null, Timeout); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Trace" && x.Message == expectedMessage); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Error" && x.Message == expectedMessage); }
public LoggingEmitter(IHubContext <LoggingHub> hub) { Hub = hub; Target = LogManager.Configuration?.AllTargets?.Select(a => a as SignalRTarget).FirstOrDefault(a => a != null); if (Target != null) { Target.LogEventHandler += OnLog; } }
public void given_proxy_creation_fails_when_writing_should_not_throw() { var target = new SignalRTarget(); Action action = () => { AsyncContinuation continuation = ex => { }; target.WriteAsyncLogEvent(new AsyncLogEventInfo(new LogEventInfo(LogLevel.Trace, "Logger", "This is the message"), continuation)); target.Flush(continuation); Thread.Sleep(1000); }; action.ShouldNotThrow(); }
public void given_nlog_configured_to_use_signalr_target_for_wrong_hub_uri_when_logging_events_should_not_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = "http://localhost:1450", Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample trace message."; Logger.Trace(expectedMessage); Wait.For(1).Seconds(); Test.Current.SignalRLogEvents.Should().NotContain(x => x.Level == "Trace" && x.Message == expectedMessage); }
public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_events_should_not_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = InProcessHubFixture.HubBaseUrl, Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample trace message."; Logger.Trace(expectedMessage); Thread.Sleep(1000); Test.Current.SignalRLogEvents.Should() .NotContain(x => x.Level == "Trace" && x.Message == expectedMessage); }
given_nlog_configured_to_use_signalr_target_for_wrong_hub_uri_when_logging_events_should_not_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = "http://localhost:1450", Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample trace message."; LogManager.GetCurrentClassLogger().Trace(expectedMessage); Thread.Sleep(1000); Test.Current.SignalRLogEvents.Should().NotContain(x => x.Level == "Trace" && x.Message == expectedMessage); }
public void given_nlog_configured_to_use_signalr_target_for_hub_with_wrong_method_name_when_logging_events_should_not_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl, MethodName = "Test", Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace); const string expectedMessage = "This is a sample trace message."; Logger.Trace(expectedMessage); Wait.For(1).Seconds(); Test.Current.SignalRLogEvents.Should().NotContain(x => x.Level == "Trace" && x.Message == expectedMessage); }
public async Task given_nlog_configured_to_use_signalr_target_for_hub() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl + "Logging", Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target); const string expectedMessage = "This is a sample trace message."; Logger.Info(expectedMessage); LogManager.Shutdown(); Assert.True(LoggingHub.LogEvents.TryDequeue(out var logEvent)); Assert.AreEqual("Info", logEvent.Level); Assert.AreEqual(expectedMessage, logEvent.Message); }
public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_event_should_log_to_signalr2() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl, Layout = "${event-properties:item=MyProperty}" }; SimpleConfigurator.ConfigureForTargetLogging(target); const string expectedMessage = "Hello, world."; const string expectedProperty = "Goodbye, world."; var logEvent = new LogEventInfo(LogLevel.Info, Logger.Name, expectedMessage); logEvent.Properties["MyProperty"] = expectedProperty; Logger.Info(logEvent); Wait.Until(() => Test.Current.SignalRLogEvents.FirstOrDefault(x => x.Level == "Info") != null, TimeSpan.FromSeconds(10)); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Info" && x.Message == expectedProperty); }
public void given_nlog_configured_to_use_signalr_target_for_hub_when_logging_events_from_multiple_threads_should_log_to_signalr() { var target = new SignalRTarget { Name = "signalr", Uri = HubBaseUrl, Layout = "${message}" }; SimpleConfigurator.ConfigureForTargetLogging(target); const string expectedMessage = "This is a sample trace message."; var tasks = new List <Task>(); Action action1 = () => { Logger.Info(expectedMessage); Wait.Until(() => Test.Current.SignalRLogEvents.FirstOrDefault(x => x.Level == "Info" && x.Message == expectedMessage) != null, TimeSpan.FromSeconds(10)); }; tasks.Add(Task.Factory.StartNew(action1)); Action action2 = () => { Logger.Error(expectedMessage); Wait.Until(() => Test.Current.SignalRLogEvents.FirstOrDefault(x => x.Level == "Error" && x.Message == expectedMessage) != null, TimeSpan.FromSeconds(10)); }; tasks.Add(Task.Factory.StartNew(action2)); Task.WaitAll(tasks.ToArray()); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Info" && x.Message == expectedMessage); Test.Current.SignalRLogEvents.Should().Contain(x => x.Level == "Error" && x.Message == expectedMessage); }