public void AddEntry(LogEntry entry) { using (LoggerDbContext loggerDatabase = new LoggerDbContext()) { loggerDatabase.LogEntries.Add(entry); loggerDatabase.SaveChanges(); } }
public void LogEntryStoresInputText() { const string cEntryText = "This is a log entry"; LogEntry entry = new LogEntry(cEntryText); Assert.AreEqual(cEntryText, entry.Text); }
protected void Application_Error(object sender, EventArgs e) { var httpContext = ((MvcApplication)sender).Context; var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); var currentController = " "; var currentAction = " "; if (currentRouteData != null) { if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString())) { currentController = currentRouteData.Values["controller"].ToString(); } if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString())) { currentAction = currentRouteData.Values["action"].ToString(); } } var ex = Server.GetLastError(); var controller = new ErrorController(); var routeData = new RouteData(); var action = "Index"; if (ex is HttpException) { var httpEx = ex as HttpException; switch (httpEx.GetHttpCode()) { case 404: action = "NotFound"; break; default: action = "Index"; break; } } //log the exception ILogger logger = new DataBaseLogger(ConfigurationManager.MyConnectionString); LogEntry logEntry = new LogEntry(ex); logger.SendMessage(logEntry); httpContext.ClearError(); httpContext.Response.Clear(); httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500; httpContext.Response.TrySkipIisCustomErrors = true; routeData.Values["controller"] = "Error"; routeData.Values["action"] = action; controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction); ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData)); }
public void LogEntryStoresCurrentTimeWhenConstructed() { LogEntry entry = new LogEntry("test"); DateTime currentTime = DateTime.Now; double delta = 2.0 * TimeSpan.TicksPerSecond; Assert.AreEqual(currentTime.Ticks, entry.CreatedTime.Ticks, delta); }
public void LogEntryToStringDisplaysDateTimeAndEntry() { LogEntry entry = new LogEntry("This is a log entry"); string expectedString = entry.CreatedTime.ToString("dd/MM/yy HH:mm") + "> " + entry.Text; Assert.AreEqual(expectedString, entry.ToString()); }
public override void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) { return; } if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500) { return; } if (!ExceptionType.IsInstanceOfType(filterContext.Exception)) { return; } // if the request is AJAX return JSON else view. if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest") { filterContext.Result = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { error = true, message = filterContext.Exception.Message } }; } else { var controllerName = (string)filterContext.RouteData.Values["controller"]; var actionName = (string)filterContext.RouteData.Values["action"]; var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); filterContext.Result = new ViewResult { ViewName = View, MasterName = Master, ViewData = new ViewDataDictionary<HandleErrorInfo>(model), TempData = filterContext.Controller.TempData }; } LogEntry logEntry = new LogEntry(filterContext.Exception); logger.SendMessage(logEntry); filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); filterContext.HttpContext.Response.StatusCode = 500; filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }
public void DisplayLogHeaderStateOutputsTodaysLogs() { LogEntry logEntry = new LogEntry("entry"); List<LogEntry> logEntries = new List<LogEntry>() { logEntry }; IConsole mockConsole = Substitute.For<IConsole>(); ILog mockLog = Substitute.For<ILog>(); mockLog.GetEntries().Returns(logEntries); ITodoList mockTodoList = Substitute.For<ITodoList>(); DisplayLogHeaderState state = new DisplayLogHeaderState(mockConsole, mockLog, mockTodoList); state.Execute(); mockConsole.Received(1).Output("> "); mockConsole.Received(1).OutputLine("entry"); }
private void InsertLogEntry(LogEntry logEntry) { using (var conn = new SqlConnection(ConnectionLogDb)) { conn.Open(); SqlCommand cmd = new SqlCommand("ExceptionLog_Insert", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Message", logEntry.MessageText); cmd.Parameters.AddWithValue("@DateTimeSpan", logEntry.ErrorTimeSpanString); cmd.Parameters.AddWithValue("@Title", logEntry.Title); cmd.Parameters.AddWithValue("@MachineName", logEntry.MachineName); cmd.ExecuteNonQuery(); } }
public void CommandLogStateFindsEntriesFromStartDate() { LogEntry searchableEntry = new LogEntry("search term"); List<LogEntry> logEntries = new List<LogEntry>() { searchableEntry }; ILog mockLog = Substitute.For<ILog>(); mockLog.GetEntries().Returns(logEntries); IConsole mockConsole = Substitute.For<IConsole>(); DateTime startDate = DateTime.Now.AddDays(+1); DateTime endDate = DateTime.Now.AddDays(+1); mockConsole.GetInput().Returns("search term", startDate.ToString(), endDate.ToString()); ITodoList mockTodoList = Substitute.For<ITodoList>(); CommandLogState state = new CommandLogState(mockConsole, mockLog, mockTodoList); state.Input = "s"; state.Execute(); mockConsole.DidNotReceive().Output(searchableEntry.CreatedTime.ToString("dd/MM/yy HH:mm> ")); mockConsole.DidNotReceive().OutputLine(searchableEntry.Text); }
public void CommandLogStateFindsEntriesMatchingSearchTerms() { LogEntry searchableEntry = new LogEntry("search term"); LogEntry unsearchableEntry = new LogEntry("Nothing here"); List<LogEntry> logEntries = new List<LogEntry>() { searchableEntry, unsearchableEntry }; ILog mockLog = Substitute.For<ILog>(); mockLog.GetEntries().Returns(logEntries); IConsole mockConsole = Substitute.For<IConsole>(); DateTime yesterday = DateTime.Now.AddDays(-1); DateTime tomorrow = DateTime.Now.AddDays(+1); mockConsole.GetInput().Returns("search term", yesterday.ToString(), tomorrow.ToString()); ITodoList mockTodoList = Substitute.For<ITodoList>(); CommandLogState state = new CommandLogState(mockConsole, mockLog, mockTodoList); state.Input = "s"; state.Execute(); mockConsole.Received(1).Output(searchableEntry.CreatedTime.ToString("dd/MM/yy HH:mm> ")); mockConsole.Received(1).OutputLine(searchableEntry.Text); }
public void CommandLogStateSearchesEntriesWhenGivenSearchString() { LogEntry entry = new LogEntry("search term"); List<LogEntry> logEntries = new List<LogEntry>() { entry }; ILog mockLog = Substitute.For<ILog>(); mockLog.GetEntries().Returns(logEntries); IConsole mockConsole = Substitute.For<IConsole>(); DateTime yesterday = DateTime.Now.AddDays(-1); DateTime tomorrow = DateTime.Now.AddDays(+1); mockConsole.GetInput().Returns("search term", yesterday.ToString(), tomorrow.ToString()); ITodoList mockTodoList = Substitute.For<ITodoList>(); CommandLogState state = new CommandLogState(mockConsole, mockLog, mockTodoList); state.Input = "s"; state.Execute(); mockConsole.Received(1).Output("Please enter the term you wish to search for: "); mockConsole.Received(1).Output("Please enter the date to start searching from: "); mockConsole.Received(1).Output("Please enter the date to search up to: "); mockConsole.Received(3).GetInput(); mockConsole.Received(1).Output(entry.CreatedTime.ToString("dd/MM/yy HH:mm> ")); mockConsole.Received(1).OutputLine(entry.Text); }
public void LogEntryThrowsExceptionWhenGivenNullText() { LogEntry entry = new LogEntry(null); }
public void LogEntryThrowsExceptionWhenGivenEmptyText() { LogEntry entry = new LogEntry(""); }
public void SendMessage(LogEntry logEntry) { InsertLogEntry(logEntry); }