private void CreateLogEntries(string methodName, string logId, int n) { using (_Logger.BeginScope(new SimpleLoggingScopeId { ClassOrMethod = methodName, Id = logId })) { var methodNamesInStack = _MethodNamesFromStackFramesExtractor.ExtractMethodNamesFromStackFrames(); for (var counter = 0; n > 0; n--) { var message = $"Log message #{++counter}"; switch (n % 3) { case 0: _Logger.LogWarningWithCallStack(message, methodNamesInStack); break; case 1: _Logger.LogErrorWithCallStack(message, methodNamesInStack); break; default: _Logger.LogInformationWithCallStack(message, methodNamesInStack); break; } } } }
public async Task HandleFeedbackToApplicationAsync(IFeedbackToApplication feedback) { using (SimpleLogger.BeginScope(SimpleLoggingScopeId.Create("Scope"))) { var methodNamesFromStack = MethodNamesFromStackFramesExtractor.ExtractMethodNamesFromStackFrames(); switch (feedback.Type) { case FeedbackType.CommandExecutionCompleted: case FeedbackType.CommandExecutionCompletedWithMessage: { await CommandExecutionCompletedHandlerAsync(feedback); } break; case FeedbackType.CommandsEnabledOrDisabled: { await CommandsEnabledOrDisabledHandlerAsync(); } break; case FeedbackType.LogInformation: { SimpleLogger.LogInformationWithCallStack(feedback.Message, methodNamesFromStack); } break; case FeedbackType.LogWarning: { SimpleLogger.LogWarningWithCallStack(feedback.Message, methodNamesFromStack); } break; case FeedbackType.LogError: { SimpleLogger.LogErrorWithCallStack(feedback.Message, methodNamesFromStack); } break; case FeedbackType.CommandIsDisabled: { SimpleLogger.LogErrorWithCallStack("Attempt to run disabled command " + feedback.CommandType, methodNamesFromStack); } break; case FeedbackType.ImportantMessage: { var fileName = feedback.Message; if (File.Exists(fileName)) { var folder = new Folder(Folder.Text); fileName = fileName.Substring(folder.FullName.Length + 1); Results.Items.Add(fileName); } } break; default: { throw new NotImplementedException(); } } } await Task.CompletedTask; }
public void Log_CalledManyTimes_IsWorking() { _Sut = new SimpleLogger(CreateLogConfiguration(nameof(Log_CalledManyTimes_IsWorking)), _Flusher, _MethodNamesFromStackFramesExtractor); using (_Sut.BeginScope(new SimpleLoggingScopeId { ClassOrMethod = "Scope", Id = "A" })) { using (_Sut.BeginScope(new SimpleLoggingScopeId { ClassOrMethod = "Scope", Id = "B" })) { for (var i = 0; i < NumberOfLogEntries; i++) { _Sut.LogInformationWithCallStack(NotAMessage, _MethodNamesFromStackFramesExtractor.ExtractMethodNamesFromStackFrames()); } } } var logEntries = _Sut.FindLogEntries(_ => true); Assert.AreEqual(NumberOfLogEntries, logEntries.Count); Assert.AreEqual(LogLevel.Information, logEntries[0].LogLevel); Assert.AreEqual(2, logEntries[0].Stack.Count); Assert.AreEqual("Scope(A)", logEntries[0].Stack[0]); Assert.AreEqual("Scope(B)", logEntries[0].Stack[1]); Assert.AreEqual(NotAMessage, logEntries[0].Message); var fileNames = _Flusher.FileNames; Assert.AreEqual(1, fileNames.Count); var fileName = fileNames.First(); Assert.IsTrue(File.Exists(fileName)); Assert.IsTrue(fileName.EndsWith(@"\Scope(A).log")); Assert.AreEqual(0, logEntries.Count(e => !e.Flushed)); File.SetLastWriteTime(fileName, DateTime.Now.AddHours(-25)); SimpleLogFlusher.ResetCleanupTime(); _Flusher.Flush(_Sut, _Sut.LogSubFolder); Assert.IsFalse(File.Exists(fileName)); }
protected async Task WorkAsync() { var methodNamesFromStack = MethodNamesFromStackFramesExtractor.ExtractMethodNamesFromStackFrames(); Assert.IsTrue(methodNamesFromStack.Contains(nameof(WorkAsync))); SimpleLogger.LogWarningWithCallStack($"{WorkerId} is working hard", methodNamesFromStack); SimpleLogger.LogInformationWithCallStack($"{WorkerId} starts working", methodNamesFromStack); while (DateTime.Now < EndOfWork) { SimpleLogger.LogInformationWithCallStack($"{WorkerId} does something", methodNamesFromStack); await Task.Delay(LogEvery); } SimpleLogger.LogErrorWithCallStack($"{WorkerId} wants to work more", methodNamesFromStack); }
public async Task <DvinApp> GetTashAppAsync(IErrorsAndInfos errorsAndInfos) { using (_SimpleLogger.BeginScope(SimpleLoggingScopeId.Create(nameof(GetTashAppAsync)))) { var methodNamesFromStack = _MethodNamesFromStackFramesExtractor.ExtractMethodNamesFromStackFrames(); _SimpleLogger.LogInformationWithCallStack("Returning tash app", methodNamesFromStack); return(await DvinRepository.LoadAsync(TashAppId, errorsAndInfos)); } }