增加功能:增加数据库错误日志记录功能

This commit is contained in:
Argo-MacBookPro 2018-06-12 11:17:21 +08:00
parent 8e89d67e0c
commit e6849053ea
3 changed files with 83 additions and 126 deletions

View File

@ -1,10 +1,12 @@
using Bootstrap.Security.Filter;
using Bootstrap.DataAccess;
using Bootstrap.Security.Filter;
using Bootstrap.Security.Middleware;
using Longbow.Cache;
using Longbow.Cache.Middleware;
using Longbow.Configuration;
using Longbow.Data;
using Longbow.Logging;
using Longbow.Web;
using Longbow.Web.WebSockets;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
@ -15,6 +17,7 @@ using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Specialized;
using System.IO;
namespace Bootstrap.Admin
@ -32,7 +35,7 @@ namespace Bootstrap.Admin
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddLogging(builder => builder.AddFileLogger());
services.AddLogging(builder => builder.AddFileLogger().AddDBLogger(ExceptionHelper.Log));
services.AddConfigurationManager();
services.AddCacheManager();
services.AddDBAccessFactory();
@ -43,7 +46,7 @@ namespace Bootstrap.Admin
services.AddMvc(options =>
{
options.Filters.Add<BootstrapAdminAuthorizeFilter>();
options.Filters.Add<BootstrapAdminExceptionFilter>();
options.Filters.Add<ExceptionFilter>();
}).AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();

View File

@ -24,9 +24,8 @@ namespace Bootstrap.DataAccess
/// <param name="ex"></param>
/// <param name="additionalInfo"></param>
/// <returns></returns>
public static bool Log(Exception ex, NameValueCollection additionalInfo)
public static void Log(Exception ex, NameValueCollection additionalInfo)
{
bool ret = false;
try
{
var sql = "insert into Exceptions (AppDomainName, ErrorPage, UserID, UserIp, ExceptionType, Message, StackTrace, LogTime) values (@AppDomainName, @ErrorPage, @UserID, @UserIp, @ExceptionType, @Message, @StackTrace, GetDate())";
@ -41,13 +40,11 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@StackTrace", DBAccessFactory.ToDBValue(ex.StackTrace)));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
}
catch (Exception e)
{
throw new Exception("Excetion Log Error", e);
}
return ret;
}
/// <summary>
/// 查询所有异常

View File

@ -33,26 +33,23 @@ namespace Bootstrap.DataAccess
{
List<User> users = new List<User>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null");
try
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
while (reader.Read())
{
while (reader.Read())
users.Add(new User()
{
users.Add(new User()
{
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = (DateTime)reader[3],
ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue),
ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5],
Description = (string)reader[6]
});
}
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = (DateTime)reader[3],
ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue),
ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5],
Description = (string)reader[6]
});
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return users;
});
}
@ -67,24 +64,20 @@ namespace Bootstrap.DataAccess
string sql = "select ID, UserName, DisplayName, RegisterTime, [Description] from Users Where ApprovedTime is null and RejectedTime is null order by RegisterTime desc";
List<User> users = new List<User>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
while (reader.Read())
{
while (reader.Read())
users.Add(new User()
{
users.Add(new User()
{
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = (DateTime)reader[3],
Description = (string)reader[4]
});
}
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
RegisterTime = (DateTime)reader[3],
Description = (string)reader[4]
});
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return users;
});
}
@ -95,21 +88,14 @@ namespace Bootstrap.DataAccess
public static bool DeleteUser(IEnumerable<int> value)
{
bool ret = false;
try
var ids = string.Join(",", value);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers"))
{
var ids = string.Join(",", value);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers"))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(userIds: ids);
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(userIds: ids);
ret = true;
return ret;
}
/// <summary>
@ -127,30 +113,23 @@ namespace Bootstrap.DataAccess
p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt);
}
bool ret = false;
try
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers"))
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers"))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", p.Id));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", DBAccessFactory.ToDBValue(p.UserName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@password", DBAccessFactory.ToDBValue(p.Password)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@passSalt", DBAccessFactory.ToDBValue(p.PassSalt)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@displayName", DBAccessFactory.ToDBValue(p.DisplayName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", DBAccessFactory.ToDBValue(p.Description)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedBy", DBAccessFactory.ToDBValue(p.ApprovedBy)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedBy", DBAccessFactory.ToDBValue(p.RejectedBy)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedReason", DBAccessFactory.ToDBValue(p.RejectedReason)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userStatus", p.UserStatus));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? string.Empty : p.Id.ToString());
ret = true;
if (p.UserStatus == 1) NotificationHelper.MessagePool.Add(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) });
}
catch (DbException ex)
{
ExceptionManager.Publish(ex);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", p.Id));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", DBAccessFactory.ToDBValue(p.UserName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@password", DBAccessFactory.ToDBValue(p.Password)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@passSalt", DBAccessFactory.ToDBValue(p.PassSalt)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@displayName", DBAccessFactory.ToDBValue(p.DisplayName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", DBAccessFactory.ToDBValue(p.Description)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedBy", DBAccessFactory.ToDBValue(p.ApprovedBy)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedBy", DBAccessFactory.ToDBValue(p.RejectedBy)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedReason", DBAccessFactory.ToDBValue(p.RejectedReason)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userStatus", p.UserStatus));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? string.Empty : p.Id.ToString());
ret = true;
if (p.UserStatus == 1) NotificationHelper.MessagePool.Add(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) });
return ret;
}
/// <summary>
@ -167,23 +146,19 @@ namespace Bootstrap.DataAccess
string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @RoleID where u.ApprovedTime is not null";
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId));
try
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
while (reader.Read())
{
while (reader.Read())
users.Add(new User()
{
users.Add(new User()
{
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]
});
}
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]
});
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return users;
}, RetrieveUsersByRoleIdDataKey);
}
@ -225,8 +200,8 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
throw ex;
}
}
return ret;
@ -245,23 +220,19 @@ namespace Bootstrap.DataAccess
string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID =@groupId where u.ApprovedTime is not null";
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", groupId));
try
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
while (reader.Read())
{
while (reader.Read())
users.Add(new User()
{
users.Add(new User()
{
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]
});
}
Id = (int)reader[0],
UserName = (string)reader[1],
DisplayName = (string)reader[2],
Checked = (string)reader[3]
});
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return users;
}, RetrieveUsersByRoleIdDataKey);
}
@ -303,8 +274,8 @@ namespace Bootstrap.DataAccess
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
throw ex;
}
}
return ret;
@ -318,22 +289,15 @@ namespace Bootstrap.DataAccess
public static bool SaveUserIconByName(string userName, string iconName)
{
bool ret = false;
try
string sql = "Update Users set Icon = @iconName where UserName = @userName";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
string sql = "Update Users set Icon = @iconName where UserName = @userName";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
CacheCleanUtility.ClearCache(cacheKey: key);
ret = true;
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
CacheCleanUtility.ClearCache(cacheKey: key);
ret = true;
}
return ret;
}
@ -346,22 +310,15 @@ namespace Bootstrap.DataAccess
public static bool SaveUserCssByName(string userName, string cssName)
{
bool ret = false;
try
string sql = "Update Users set Css = @cssName where UserName = @userName";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
string sql = "Update Users set Css = @cssName where UserName = @userName";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cssName", DBAccessFactory.ToDBValue(cssName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
CacheCleanUtility.ClearCache(cacheKey: key);
ret = true;
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cssName", DBAccessFactory.ToDBValue(cssName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
CacheCleanUtility.ClearCache(cacheKey: key);
ret = true;
}
return ret;
}