public void OnException(ExceptionContext context) { ResponseModel responseModel = new ResponseModel(); responseModel.Code = ResponseCode.Error; responseModel.Message = context.Exception.Message; //自定义的操作记录日志 if (context.Exception.GetType() == typeof(UserOperationException)) { if (_env.IsDevelopment()) { //堆栈信息 responseModel.Data = context.Exception.StackTrace.TrimStart(); } //返回异常数据 context.Result = new BadRequestObjectResult(responseModel); } else { if (_env.IsDevelopment()) { //堆栈信息 responseModel.Data = context.Exception.StackTrace.TrimStart(); } context.Result = new InternalServerErrorObjectResult(responseModel); } //日志记录 NLogHelper.ErrorLog(responseModel.Message, context.Exception); }
/// <summary> /// 事务处理 /// </summary> /// <param name="action">委托</param> /// <param name="iso">隔离级别</param> /// <returns></returns> public async Task <bool> ActionTran(Func <Task <bool> > action, IsolationLevel iso = IsolationLevel.ReadCommitted) { try { BaseDal.BeginTran(iso); await action(); BaseDal.CommitTran(); return(true); } catch (Exception ex) { NLogHelper.ErrorLog(ex.Message, ex); BaseDal.RollbackTran(); return(false); } }
/// <summary> /// Excel导出 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="fileData">数据源</param> /// <param name="fileName">导出文件名称(注意唯一)</param> /// <param name="filePath">数据源</param> /// <returns></returns> public async Task <bool> ExcelExport <T>(List <T> fileData, string fileName, string filePath = "") where T : class, new() { try { string curDir = string.Empty; // 默认导出路径 if (String.IsNullOrEmpty(filePath)) { curDir = AppContext.BaseDirectory + "/Export"; } else { curDir = filePath; } // 文件夹是否存在 if (Directory.Exists(curDir)) { Directory.CreateDirectory(curDir); } string fileUrl = Path.Combine(curDir, fileName + ".xlsx"); var bytes = await _excelExportService.ExportAsync(new ExportOption <T>() { // 数据源 Data = fileData, // 数据行起始索引,默认1 DataRowStartIndex = 1, // 导出Excel类型,默认xls // ExcelType = Bayantu.Extensions.Office.Enums.ExcelTypeEnum.XLS, // 表头行索引,默认0 HeaderRowIndex = 0, // 页签名称,默认sheet1 SheetName = "sheet1" }); File.WriteAllBytes(fileUrl, bytes); return(true); } catch (Exception ex) { NLogHelper.ErrorLog(ex.Message, ex); return(false); } }
/// <summary> /// 通道中间件 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task InvokeAsync(HttpContext context) { // 过滤 if (context.Request.Path.Value.ToLower().Contains("api")) { context.Request.EnableBuffering(); Stream originalBody = context.Response.Body; try { // 请求 RequestDataLog(context.Request); using (var ms = new MemoryStream()) { context.Response.Body = ms; await _next(context); // 响应 ResponseDataLog(context.Response, ms); ms.Position = 0; await ms.CopyToAsync(originalBody); } } catch (Exception ex) { // 记录 NLogHelper.ErrorLog(context.Response.ToString(), ex); } finally { context.Response.Body = originalBody; } } else { await _next(context); } }
public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); // 生成承载 web 应用程序的 Microsoft.AspNetCore.Hosting.IWebHost var host = CreateWebHostBuilder(args).Build(); // 创建可用于解析作用域的服务 using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var loggerFactory = services.GetRequiredService <ILoggerFactory>(); // get the IpPolicyStore instance var ipPolicyStore = scope.ServiceProvider.GetRequiredService <IIpPolicyStore>(); // seed IP data from appsettings ipPolicyStore.SeedAsync(); try { // 从 system.IServicec提供程序获取 T 类型的服务 // 为了大家的数据安全,这里先注释掉了 // 数据库连接字符串是在Entity层的Seed 文件夹下的 DbContext.cs 中 var configuration = services.GetRequiredService <IConfiguration>(); if (configuration.GetSection("AppSettings")["SeedDBEnabled"].ToBool()) { var dbContext = services.GetRequiredService <DbContext>(); } } catch (Exception e) { NLogHelper.ErrorLog("数据库初始化失败。", e); throw; } } // 运行 web 应用程序并阻止调用线程, 直到主机关闭 // 创建完 WebHost 之后,便调用它的 Run 方法,而 Run 方法会去调用 WebHost 的 StartAsync 方法 // 将Initialize方法创建的Application管道传入以供处理消息 // 执行HostedServiceExecutor.StartAsync方法 host.Run(); }
/// <summary> /// Excel导入 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="filePath">Excel绝对路径</param> /// <returns></returns> public async Task <DataTable> ExcelImport <T>(string filePath) where T : class, new() { var _rows = await _excelImportService.ValidateAsync <T>(new ImportOption() { //Excel文件绝对地址 FileUrl = filePath, //数据起始行索引,默认1第二行 DataRowStartIndex = 1, //表头起始行索引,默认0第一行 HeaderRowIndex = 0, //映射字典,可以将模板类与Excel列重新映射, 默认null MappingDictionary = null, //页面索引,默认0第一个页签 SheetIndex = 0, //校验模式,默认StopOnFirstFailure校验错误后此行停止继续校验,Continue:校验错误后继续校验 ValidateMode = ValidateModeEnum.Continue }); //数据校验 var errorDatas = _rows.Where(x => !x.IsValid); if (errorDatas.Count() > 0) { NLogHelper.ErrorLog("Excel导入验证失败。"); return(null); } //导入转成DataTable return(await _excelImportService.ToTableAsync <T> ( //文件绝对地址 filePath, //页签索引,默认0 0, //表头行索引,默认0 0, //数据行索引,默认1 1, //读取多少条数据,默认-1全部 -1)); }
public void OnException(ExceptionContext context) { //采用NLog 进行错误日志记录 NLogHelper.ErrorLog(context.Exception.Message, context.Exception); }
public void OnException(ExceptionContext context) { NLogHelper.ErrorLog(context.Exception); }