public void When_logging_using_error() { _logger.Error("Hi"); _mockedInnerLogger.Verify(l => l.Log(typeof(Log4NetLogger), Level.Error, "Hi", null), Times.Once); var ex = new InvalidOperationException(); _logger.Error("Ooops", ex); _mockedInnerLogger.Verify(l => l.Log(typeof(Log4NetLogger), Level.Error, "Ooops", ex), Times.Once); _logger.ErrorFormat("E:{0}", 1); _mockedInnerLogger.Verify(l => l.Log(typeof(Log4NetLogger), Level.Error, It.IsAny <string>(), null), Times.Exactly(2)); _logger.ErrorFormat("E:{0}, {1}", 1, 2); _mockedInnerLogger.Verify(l => l.Log(typeof(Log4NetLogger), Level.Error, It.IsAny <string>(), null), Times.Exactly(3)); _logger.ErrorFormat("E:{0}, {1}, {2}", 1, 2, 3); _mockedInnerLogger.Verify(l => l.Log(typeof(Log4NetLogger), Level.Error, It.IsAny <string>(), null), Times.Exactly(4)); _logger.ErrorFormat("E:{0}, {1}, {2}, {3}, {4}", 1, 2, 3, 4, 5); _mockedInnerLogger.Verify(l => l.Log(typeof(Log4NetLogger), Level.Error, It.IsAny <string>(), null), Times.Exactly(5)); var italianCulture = new CultureInfo("it-It"); var date = new DateTime(2000, 12, 28, 1, 4, 43, 0); _logger.ErrorFormat(italianCulture, "Date: {0}", date); _mockedInnerLogger.Verify(l => l.Log(typeof(Log4NetLogger), Level.Error, It.IsAny <string>(), null), Times.Exactly(6)); }
public void OnException(ExceptionContext context) { HttpStatusCode status = HttpStatusCode.InternalServerError; String message = String.Empty; HttpResponse response = context.HttpContext.Response; response.ContentType = "application/json"; var exceptionType = context.Exception.GetType(); IEasyLogger logger = logService.GetLogger <Program>(); context.ExceptionHandled = true; if (exceptionType == typeof(OperationException)) { message = ((OperationException)context.Exception).ErrorMessage; status = (((OperationException)context.Exception).ErrorStatus == 400) ? HttpStatusCode.BadRequest : HttpStatusCode.InternalServerError; dynamic json = new JObject(); json.ErrorMessage = message; json.ErrorStatus = status; json.ErrorCode = ((OperationException)context.Exception).ErrorCode; response.StatusCode = (int)status; var err = ((JObject)json).ToString(); logger.Error(err); response.WriteAsync(err); } else { if (exceptionType == typeof(UnauthorizedAccessException)) { message = "Unauthorized Access"; status = HttpStatusCode.Unauthorized; } else if (exceptionType == typeof(NotImplementedException)) { message = "A server error occurred."; status = HttpStatusCode.NotImplemented; } else { message = context.Exception.Message; status = HttpStatusCode.NotFound; } response.StatusCode = (int)status; var err = message; logger.Error(err); response.WriteAsync(err); } }
private void Log(string message) { switch (_level) { case EasyLogLevel.Debug: _logger.Debug(message); break; case EasyLogLevel.Info: _logger.Info(message); break; case EasyLogLevel.Warn: _logger.Warn(message); break; case EasyLogLevel.Error: _logger.Error(message); break; case EasyLogLevel.Fatal: _logger.Fatal(message); break; } }