示例#1
0
        //SET ANSI_NULLS ON
        //  SET QUOTED_IDENTIFIER ON
        //  CREATE TABLE [dbo].[Logs] (
        //	  [Id] [int] IDENTITY(1,1) NOT NULL,
        //	  [MachineName] [nvarchar](50) NOT NULL,
        //	  [Logged] [datetime] NOT NULL,
        //	  [Level] [nvarchar](50) NOT NULL,
        //	  [Message] [nvarchar](max) NOT NULL,
        //	  [UserId] [nvarchar](100),
        //	  [UserName] [nvarchar](100),
        //	  [Data] [nvarchar](max),
        //	  [Logger] [nvarchar](250) NULL,
        //	  [Callsite] [nvarchar](max) NULL,
        //	  [Exception] [nvarchar](max) NULL,
        //    CONSTRAINT [PK_dbo.Log] PRIMARY KEY CLUSTERED ([Id] ASC)
        //      WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
        //  ) ON [PRIMARY]

        public static void ConfigureLogging()
        {
            var config = new NLog.Config.LoggingConfiguration();

            // Targets where to log to: File and Console
            var nConf = App.Instance.NLog;
            var logDb = new NLog.Targets.DatabaseTarget()
            {
                Name             = nConf.Name,
                DBProvider       = nConf.DBProvider,
                ConnectionString = nConf.ConnectionString,
                CommandType      = nConf.CommandType,
                CommandText      = nConf.CommandText,
            };

            foreach (var para in nConf.Parameters)
            {
                logDb.Parameters.Add(new NLog.Targets.DatabaseParameterInfo(para.Name, para.Layout));
            }
            // Rules for mapping loggers to targets
            config.AddRule(LogLevel.Info, LogLevel.Fatal, logDb);

            // Apply config
            NLog.LogManager.Configuration = config;
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //https://www.cnblogs.com/jimmyLei/p/11284598.html
            //log.AddNLog();
            //env.ConfigureNLog("NLog.config");//配置NLog文件

            #region 将日志记录到数据库

            var NLOGDataBase = Configuration.GetConnectionString("DefaultConnection");

            //var configFileName = $"nlog.Joson{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.config";
            //NLogs.LogManager.LoadConfiguration(configFileName).GetCurrentClassLogger();


            NLogs.LogManager.Configuration.Variables["connectionString"] = NLOGDataBase;
            NLogs.Targets.DatabaseTarget databaseTarget = NLogs.LogManager.Configuration.FindTargetByName <NLogs.Targets.DatabaseTarget>("Database");
            databaseTarget.ConnectionString = Configuration.GetConnectionString("DefaultConnection");


            NLogs.LogManager.Configuration.Variables["NLOG_CONNECTION_STRING"] = NLOGDataBase;
            NLogs.LogManager.Configuration.FindTargetByName <NLogs.Targets.DatabaseTarget>("SQLServerCreateNLog").ConnectionString = Configuration.GetConnectionString("DefaultConnection");

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);  //避免日志中的中文输出乱码

            #endregion

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }