/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/ BEGIN TRANSACTION SET QUOTED_IDENTIFIER ON SET ARITHABORT ON SET NUMERIC_ROUNDABORT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ANSI_NULLS ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON COMMIT BEGIN TRANSACTION GO CREATE TABLE dbo.Log ( LogID bigint NOT NULL IDENTITY (1, 1), UserName nchar(100) NOT NULL, IP nchar(20) NOT NULL, Controller nchar(200) NOT NULL, Action nchar(100) NOT NULL, Number int NOT NULL, Field nchar(100) NOT NULL, Value nchar(1000) NOT NULL, CreateDate datetime NOT NULL, GUID nchar(40) NOT NULL ) ON [PRIMARY] GO ALTER TABLE dbo.Log ADD CONSTRAINT DF_Log_CreateDate DEFAULT getdate() FOR CreateDate GO ALTER TABLE dbo.Log ADD CONSTRAINT PK_Log PRIMARY KEY CLUSTERED ( LogID ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] GO ALTER TABLE dbo.Log SET (LOCK_ESCALATION = TABLE) GO COMMIT
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Linq.Mapping; namespace %Namespace%.Models.Log { [Table(Name="Log")] public class Log { [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] public long LogID { get; set; } [Column()] public string UserName { get; set; } [Column()] public string IP { get; set; } [Column()] public string Controller { get; set; } [Column()] public string Action { get; set; } [Column()] public int Number { get; set; } [Column()] public string Field { get; set; } [Column()] public string Value { get; set; } [Column()] public string GUID { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Linq; namespace %Namespace%.Models { public class LogRepository { protected Table< Log > table; public LogRepository (DataContext dataContext) { table = dataContext.GetTable< Log >(); } public void Add(Log entity) { table.InsertOnSubmit(entity); } public void SubmitChanges() { table.Context.SubmitChanges(); } } }
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) { if (Request.RequestContext.RouteData.Values["controller"] == null) return; LogRepository logRepo = new LogRepository(new DataContext(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString)); string Controller = (Request.RequestContext.RouteData.Values["controller"]!=null?Request.RequestContext.RouteData.Values["controller"].ToString():"PanelSpecifications"); string Action = (Request.RequestContext.RouteData.Values["action"]!=null?Request.RequestContext.RouteData.Values["action"].ToString():"Index"); int counter = 0; string IP = Request.UserHostAddress; string UserName = User.Identity.Name; string GUID = Guid.NewGuid().ToString(); // start mark logRepo.Add(new Log() { Controller = Controller, Action = Action, IP = IP, UserName = UserName, Number = counter, Field = "StartLog", Value = "StartLog", GUID = GUID } ); counter++; // full route data foreach (var item in Request.RequestContext.RouteData.Values) { if(item.Key.Trim().ToLower() != "controller" && item.Key.Trim().ToLower() != "action") { logRepo.Add(new Log() { Controller = Controller, Action = Action, IP = IP, UserName = UserName, Number = counter, Field = item.Key??"", Value = Convert.ToString(item.Value)??"" , GUID = GUID } ); counter++; } } // Request Query String foreach (string key in Request.QueryString.Keys) { string Value = Convert.ToString(Request.QueryString[key]); logRepo.Add(new Log() { Controller = Controller, Action = Action, IP = IP, UserName = UserName, Number = counter, Field = key??"", Value = Value??"" , GUID = GUID } ); counter++; } // Request Form Values foreach (string key in Request.Form.Keys) { string Value = Convert.ToString(Request.Form[key]); logRepo.Add(new Log() { Controller = Controller, Action = Action, IP = IP, UserName = UserName, Number = counter, Field = key??"", Value = Value??"" , GUID = GUID } ); counter++; } // save changes logRepo.SubmitChanges(); }
Source: https://habr.com/ru/post/127784/
All Articles