Esempio n. 1
0
        /// <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;
            }
        }
Esempio n. 2
0
        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();
            }
        }