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()); } }
/// <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 static void Main(string[] args) { var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); var host = CreateHostBuilder(args).Build();//.Run(); try { using (IServiceScope scope = host.Services.CreateScope()) { IConfiguration configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>(); string sqlString = configuration.GetSection("ConnectionStrings:MySqlConnection").Value; NLogUtil.EnsureNlogConfig("Nlog.config", sqlString); } logger.Info("网站启动成功"); host.Run(); } catch (Exception ex) { string errorMessage = "网站启动初始化数据异常"; NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Web, errorMessage, new Exception(errorMessage, ex)); NLogUtil.WriteDBLog(NLog.LogLevel.Error, LogType.Web, errorMessage, new Exception(errorMessage, ex)); throw; } finally { //确保在应用程序退出之前停止内部计时器/线程(避免在Linux上出现分段错误) NLog.LogManager.Shutdown(); } }
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); }
public static void AddSqlSugarSetup(this IServiceCollection services) { if (services == null) { throw new ArgumentNullException(nameof(services)); } string connectionString = AppSettingsHelper.GetContent("ConnectionStrings", "SqlServerConnection"); string connId = AppSettingsHelper.GetContent("ConnectionStrings", "ConnId"); services.AddScoped <ISqlSugarClient>(o => { //RedisCache是继承ICacheService自已实现的一个类 //ICacheService myCache = new SqlSugarRedisCache("127.0.0.1"); //ICacheService myCache = new SqlSugarHttpRuntimeCache(); var db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = connectionString, //必填 DbType = SqlSugar.DbType.SqlServer, //必填 IsAutoCloseConnection = true, //默认false (设为true我们不需要close或者Using的操作,比较推荐) InitKeyType = InitKeyType.Attribute, MoreSettings = new ConnMoreSettings() { IsWithNoLockQuery = true //为true表式查询的时候默认会加上.With(SqlWith.NoLock),可以用With(SqlWith.Null)让全局的失效 } //InitKeyType = InitKeyType.SystemTable, //ConfigureExternalServices = new ConfigureExternalServices() //{ // DataInfoCacheService = myCache //}, //设为true相同线程是同一个SqlConnection //IsShardSameThread = true //读写分离 HitRate表示权重 值越大执行的次数越高,如果想停掉哪个连接可以把HitRate设为0 //SlaveConnectionConfigs = new List<SlaveConnectionConfig>() { // new SlaveConnectionConfig() { HitRate=10, ConnectionString=DataBaseOperationConfig.ConnectionString2 }, // new SlaveConnectionConfig() { HitRate=30, ConnectionString=DataBaseOperationConfig.ConnectionString3 } //} }); //默认SystemTable //日志处理 ////SQL执行前 可以修改SQL //db.Aop.OnLogExecuting = (sql, pars) => //{ // //获取sql // Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); // Console.WriteLine(); // //通过TempItems这个变量来算出这个SQL执行时间(1) // if (db.TempItems == null) db.TempItems = new Dictionary<string, object>(); // db.TempItems.Add("logTime", DateTime.Now); // //通过TempItems这个变量来算出这个SQL执行时间(2) // var startingTime = db.TempItems["logTime"]; // db.TempItems.Remove("time"); // var completedTime = DateTime.Now; //}; //db.Aop.OnLogExecuted = (sql, pars) => //SQL执行完事件 //{ //}; //db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前事件 //{ //}; db.Aop.OnError = (exp) => //执行SQL 错误事件 { NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "执行SQL错误事件", exp); }; return(db); }); }