public void Test_log4net_InsertOnStartReplaceOnEnd() { Audit.Core.Configuration.Setup() .UseLog4net(_ => _ .LogLevel(LogLevel.Info)); _adapter.Clear(); using (var s = new AuditScopeFactory().Create(new AuditScopeOptions() { CreationPolicy = EventCreationPolicy.InsertOnStartReplaceOnEnd, EventType = "Test_log4net_InsertOnStartReplaceOnEnd" })) { } var events = _adapter.PopAllEvents(); Assert.AreEqual(2, events.Length); Assert.AreEqual("Test_log4net_InsertOnStartReplaceOnEnd", JsonAdapter.Deserialize <AuditEvent>(events[0].MessageObject.ToString()).EventType); Assert.AreEqual("Test_log4net_InsertOnStartReplaceOnEnd", JsonAdapter.Deserialize <AuditEvent>(events[1].MessageObject.ToString()).EventType); var jsonAdapter = new JsonAdapter(); Assert.AreEqual(jsonAdapter.Deserialize <AuditEvent>(events[0].MessageObject.ToString()).CustomFields["EventId"].ToString(), jsonAdapter.Deserialize <AuditEvent>(events[1].MessageObject.ToString()).CustomFields["EventId"].ToString()); }
public void TestThreadSafety() { ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); var memoryAppender = new MemoryAppender(); var patternLayout = new PatternLayout(); memoryAppender.Layout = patternLayout; memoryAppender.ActivateOptions(); BasicConfigurator.Configure(rep, memoryAppender); cThreadsRunning = cThreadsMax; var threads = Enumerable.Range(0, cThreadsMax) .Select(i => new Thread(LogMessages(rep.Name))) .ToList(); foreach (var thread in threads) { thread.Start(); } long cEventsRead = 0; while (cThreadsRunning > 0) { var events = memoryAppender.PopAllEvents(); cEventsRead += events.Length; } foreach (var thread in threads) { thread.Join(); } cEventsRead += memoryAppender.PopAllEvents().Length; Assert.AreEqual(cEventsExpected, cEventsRead, "Log events were lost."); }
private async void button5_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; button5.Enabled = false; Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy; MemoryAppender mappender = hierarchy.Root.GetAppender("MemoryAppender") as MemoryAppender; try { var dL = checkBox1.Checked ? symbols.GetDeviceLists() : db.GetDeviceLists(); if (dL == null) { MessageBox.Show("Nepodařilo se získat seznam zařízení. Generování zastaveno.", "Upozornění", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } mappender.Clear(); if (!RefreshDatablock(SelectedDb)) { if (MessageBox.Show($"Chyba při provádění refreshe databloků z projektu. Pokračovat s offline daty?", "Dotaz", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } } await PointGeneratorCimplicity.Run(SelectedDb, definitions, pointStructures, dL); } catch (AggregateException ex) { MessageBox.Show(string.Join("\n", ex.InnerExceptions.Select(x => x.Message)), "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show(string.Join("\n", ex.Message), "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { button5.Enabled = true; Cursor.Current = Cursors.Default; } var ev = mappender.PopAllEvents(); if (ev != null && ev.Length > 0) { if (MessageBox.Show("Během generování se vyskytly chyby. Chceš je zobrazit?", "Upozornění", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { var dlg = new LogFrm(ev.OrderBy(x => x.Properties["datablock"]).Select(x => x.RenderedMessage).ToArray()); dlg.Show(this); } } }
public void CrossCuttingLogging_LogsDebugMessagesSuccessfully() { const string debugTestMessage1 = "Debug Test Message 1"; Log.Debug(debugTestMessage1); const string debugTestMessage2 = "Debug Test Message 2 {0}"; Log.DebugFormat(debugTestMessage2, Args); // Let it finish... Thread.Sleep(100); // Get all log Entries from the Memory Appender var logEntries = _appender.PopAllEvents(); // Check for the logged messages Assert.AreEqual(2, logEntries.Length); Assert.IsNotNull(logEntries.Single(s => s.Level == Level.Debug && s.RenderedMessage.Contains(debugTestMessage1))); Assert.IsNotNull(logEntries.Single(s => s.Level == Level.Debug && s.RenderedMessage.Contains(debugTestMessage2.Replace("{0}", Args)))); }