ismetertugral
11/24/2017 - 8:50 AM

Sql Log 1 Mvc

Sql Log 1 Mvc

public static int getDbCommandTimeOut()
{
	int value = 0;
	if (!int.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["CommandTimeout"], out value))
	value = 120;
	return value;
}

public static async Task StoreSqlLog(String UserName, String sql)
{
	string _userName = UserName ?? HttpContext.Current.Request.Headers.Get("V69DLOSn2KiWZQ23GKBZ");
	String user = (String.IsNullOrEmpty(_userName) ? HttpContext.Current.User.Identity.GetUserId() : GetUserByName(_userName).Id) ?? ("98947c8e-e209-4bf7-b970-5e1343602922");

	String text = HttpContext.Current.Request.Url.Host.ToLower();
	if (!HttpContext.Current.Request.Url.Host.ToLower().Equals("localhost"))
	{
		String ip = HttpContext.Current.Request.UserHostAddress;
		using (var dbc = new DataEntities())
		{
			SQLLog log = new SQLLog()
			{
				SQLLogIP = ip + "/M",
				SQLLogSQLKodu = sql,
				SQLLogTarih = DateTime.Now,
				SQLLogUserID = user
			};
			dbc.SQLLogs.Add(log);
			await dbc.SaveChangesAsync();
		}
	}
}

context.Database.CommandTimeout = Storage.getDbCommandTimeOut();
string sql = ;
await Storage.StoreSqlLog(null, sql);