/// <summary> /// 启动配置 /// </summary> /// <param name="args"></param> public static void Main(string[] args) { //NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); //CreateHostBuilder(args).Build().Run(); var host = CreateHostBuilder(args).Build(); try { using (var scope = host.Services.CreateScope()) { var configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>(); //获取到appsettings.json中的连接字符串 var sqlString = configuration.GetSection("ConnectionStrings:SqlServerConnection").Value; //确保NLog.config中连接字符串与appsettings.json中同步 NLogUtil.EnsureNlogConfig("NLog.config", sqlString); } //throw new Exception("测试异常");//for test //其他项目启动时需要做的事情 NLogUtil.WriteDbLog(LogLevel.Trace, LogType.Web, "网站启动成功"); NLogUtil.WriteFileLog(LogLevel.Trace, LogType.Web, "网站启动成功"); host.Run(); } catch (Exception ex) { //使用nlog写到本地日志文件(万一数据库没创建/连接成功) var errorMessage = "网站启动成功初始化数据异常"; NLogUtil.WriteFileLog(LogLevel.Error, LogType.Web, errorMessage, new Exception(errorMessage, ex)); NLogUtil.WriteDbLog(LogLevel.Error, LogType.Web, errorMessage, new Exception(errorMessage, ex)); throw; } }
public async Task <IActionResult> Refund() { try { var notify = await _client.ExecuteAsync <WeChatPayRefundNotify>(Request, _optionsAccessor.Value); NLogUtil.WriteDbLog(NLog.LogLevel.Trace, LogType.Refund, "退款结果通知" + JsonConvert.SerializeObject(notify)); NLogUtil.WriteFileLog(NLog.LogLevel.Trace, LogType.Refund, "退款结果通知" + JsonConvert.SerializeObject(notify)); if (notify.ReturnCode == WeChatPayCode.Success) { if (notify.RefundStatus == WeChatPayCode.Success) { //Console.WriteLine("OutTradeNo: " + notify.OutTradeNo); var memo = JsonConvert.SerializeObject(notify); return(WeChatPayNotifyResult.Success); } } return(NoContent()); } catch { return(NoContent()); } }
private async Task HandleExceptionAsync(HttpContext context, Exception ex) { if (ex == null) { return; } NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Web, "全局捕获异常", new Exception("全局捕获异常", ex)); NLogUtil.WriteDbLog(NLog.LogLevel.Error, LogType.Web, "全局捕获异常", new Exception("全局捕获异常", ex)); await WriteExceptionAsync(context, ex).ConfigureAwait(false); }
public void OnException(ExceptionContext context) { NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.ApiRequest, "全局捕获异常", new Exception("全局捕获异常", context.Exception)); NLogUtil.WriteDbLog(NLog.LogLevel.Error, LogType.ApiRequest, "全局捕获异常", new Exception("全局捕获异常", context.Exception)); HttpStatusCode status = HttpStatusCode.InternalServerError; //处理各种异常 var jm = new AdminUiCallBack(); jm.code = (int)status; jm.msg = "发生了全局异常请联系管理员"; jm.data = context.Exception; context.ExceptionHandled = true; context.Result = new ObjectResult(jm); }