繁体   English   中英

尝试登录到 SQL Server 数据库,但它似乎不起作用

[英]Attempting to log to a SQL Server database, but it doesn't seem to be working

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

我尝试以更接近实际情况的方式尝试此操作,但仍然没有任何运气。

信息应该在数据库中的路径。

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;

        Log.Logger = new LoggerConfiguration()
           .Enrich.FromLogContext()
           .WriteTo.MSSqlServer(connectionString: "",
                 tableName: "Logs"
                , schemaName: "LOG"
                , autoCreateSqlTable: true,
                 restrictedToMinimumLevel: LogEventLevel.Information)
           .CreateLogger();
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddSerilog();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }
}

public class ValuesController : ControllerBase
{
    private readonly ILogger<ValuesController> _logger;

    public ValuesController(ILogger<ValuesController> logger)
    {
        _logger = logger;
    }

    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        _logger.LogInformation($"Start:");
        return new string[] { "value1", "value2" };
    }
 }

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

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM