尝试登录到sql服务器数据库,但似乎不起作用

问题描述 投票:0回答:1

我正在尝试登录到SQL Server。从我发现的情况来看,它应该是正确的,但似乎仍然没有日志记录。您能看到我在这段代码中做什么吗?抱歉,将代码作为图片发布,此处的格式不适合我。

Path where the info should be going to in the database.

        public static void Main(string[] args)
    {
        var path = Directory.GetCurrentDirectory();
        var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

        var configuration = new ConfigurationBuilder()
        .SetBasePath(path)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environmentName}.json", optional: false, reloadOnChange: true)
        .Build();

        var logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .WriteTo.MSSqlServer(connectionString: "Connection String Here",
                 tableName: "Logs"
                , schemaName: "LOG"
                , autoCreateSqlTable: true,
                 restrictedToMinimumLevel: LogEventLevel.Information) 
            .WriteTo.Console()
            .CreateLogger();

        Log.Logger.Information("Testing");
        Log.Logger.Warning("Testing");
        Log.Logger.Verbose("Testing");
        Log.Logger.Debug("Testing");
        Log.Logger.Fatal("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");
        Log.Logger.Error("Testing");

        try
        {
            WebHost.CreateDefaultBuilder(args)
              .UseStartup<Startup>()
              .UseSerilog()
              .Build()
              .Run();
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }
c# sql-server asp.net-core-2.1 serilog
1个回答
0
投票

您的代码未设置默认的Serilog记录器,因此所有记录调用都将丢失。您应该添加

Log.Logger=logger;`

Serilog.AspNetCore回购页面中显示:


    public static int Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .CreateLogger();

        try
        {
            Log.Information("Starting web host");
            CreateWebHostBuilder(args).Build().Run();
            return 0;
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Host terminated unexpectedly");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseSerilog(); // <-- Add this line;
}
© www.soinside.com 2019 - 2024. All rights reserved.