System log and exception handling ①
System needs to be more and more automated, we need to introduce logging and exception capture
The Administrator's action record needs to be recorded to see which modules are frequently manipulated, what are the unnecessary functions, and which are to be optimized.
The system's exceptions need to be captured, rather than displaying system errors to the user. We need an anomaly log to continually improve the system.
We always say that the user, we do not have User rights table, so we first in the home to join a virtual user!
First we create a user class Accountmodel placed under App.models under the Sys folder
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace App.Models.Sys
{public
class Accountmodel
{public
string Id {get; set;}
public string Truename {get; set;}
}} AccountModel.cs
Inserting code in HomeController or AccountController
Accountmodel account = new Accountmodel ();
Account. Id = "admin";
Account. Truename = "admin";
session["Account"] = account;
The following will bring the system log records, the main record of the administrator of the increase, deletion, modification and other operations of the success and failure of the abnormal record
Log Plug-ins have a well-known log4net, you can output a variety of formats, such as text, XML, database, etc., we do not need to do so strong, we only do in line with the system can be, record to the database, convenient to do statistics and other exercises
When and where shall we record the log?
Make a record in the controller layer;
Record when the user's operation is successful;
Record when the user's operation fails;
First create a database repository: SysLog
Use DB
go
/****** Object: Table [dbo].[ SysLog] Script date:11/20/2013 21:13:38 ******/
SET ansi_nulls on
go
SET quoted_identifier on
go< C9/>set ansi_padding on
go
CREATE TABLE [dbo].[ SYSLOG] (
[Id] [varchar] () NOT NULL,--guid
[Operator] [varchar] NULL,--operator
[message] [varchar] ( Null,--operation information
[result] [varchar] (a) null,--results
[type] [varchar] (a) NULL,--action type
[Module] [varchar] (a) Null,--operation module
[Createtime] [datetime] NULL,--Action event
CONSTRAINT [Pk_syslog] PRIMARY KEY CLUSTERED
(
[Id] ASC
) With (pad_index = off, statistics_norecompute = off, Ignore_dup_key = off, allow_row_locks = ON, Allow_ Page_locks = on) on [PRIMARY]
) on [PRIMARY]
go
SET ansi_padding off
go
syslogsql
The EF update model, create the Syslogmodel class and place it under the Sys folder under App.models
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.ComponentModel.DataAnnotations;
Namespace App.Models.Sys
{public
class Syslogmodel
{
[Display (Name = ' id ']] public
string ID { Get Set
[Display (Name = operator)] public
string Operator {get; set;}
[Display (Name = "info")]
public string Message {get; set;}
[Display (Name = "result")]
public string result {get; set;}
[Display (Name = "type")]
public string Type {get; set;}
[Display (Name = "module")]
public string Module {get; set;}
[Display (Name = "Creation Time")]
Public DateTime? createtime {get; set;}
}} SysLogModel.cs