sql >> Database teknologi >  >> RDS >> PostgreSQL

.NET Core-logning til PostgreSQL DB ved hjælp af NLog

Dette fungerede for mig at skrive ind i filen og PostgreSQL:

  1. Først oprettede jeg en ASP.NET Core Web Application (.NET Core).
  2. Så installerede jeg fra NuGet:

  3. Ændrede følgende filer som dette:

    a) project.json

    "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true,
        "copyToOutput": {
            "includeFiles": [ "nlog.config" ]
        }
    },
    "publishOptions": {
        "include": [
            "wwwroot",
            "web.config",
            "nlog.config"
        ]
    }
    


    b) Startup.cs

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            env.ConfigureNLog("nlog.config");
        }
    
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
    
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddNLog();
            app.UseDeveloperExceptionPage();
            app.UseMvc();
            app.AddNLogWeb();
        }
    }
    


    c) nlog.config

    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          autoReload="true"
          internalLogLevel="Warn"
          internalLogFile="c:\temp\internal-nlog.txt">
    
      <!-- Load the ASP.NET Core plugin -->
      <extensions>
        <add assembly="NLog.Web.AspNetCore"/>
      </extensions>
    
      <!-- the targets to write to -->
      <targets>
        <target xsi:type="File" name="localFile" fileName="c:\\temp\\nlog-all-${shortdate}.log"
                 layout="${longdate} ${level} ${message} ${logger} ${callsite} ${exception:format=tostring}" />
    
        <target name="database" xsi:type="Database">
          <dbProvider>Npgsql.NpgsqlConnection, Npgsql, Version=3.2.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7</dbProvider>
          <connectionString>Server=localhost;Port=5432;User Id=XXX;Password=XXX;Database=TestLog;</connectionString>
          <commandText>
            insert into "logs" ("Application", "Timestamp", "Level", "Message", "Logger", "CallSite", "Exception")
            values (:Application, :Timestamp, :Level, :Message, :Logger, :Callsite, :Exception);
          </commandText>
          <parameter name="@Application" layout="TestApp" />
          <parameter name="@Timestamp" layout="${longdate}" />
          <parameter name="@Level" layout="${level}" />
          <parameter name="@Message" layout="${message}" />
          <parameter name="@Logger" layout="${logger}" />
          <parameter name="@Callsite" layout="${callsite}" />
          <parameter name="@Exception" layout="${exception:format=tostring}" />
        </target>
    
        <!-- write to the void aka just remove -->
        <target xsi:type="Null" name="blackhole" />
      </targets>
    
      <!-- rules to map from logger name to target -->
      <rules>
        <!--All logs, including from Microsoft-->
        <logger name="*" minlevel="Debug" writeTo="database" />
        <logger name="*" minlevel="Debug" writeTo="localFile" />
    
        <!--Skip Microsoft logs and so log only own logs-->
        <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
      </rules>
    </nlog>
    


    d) Controller

    public class HomeController : Controller
    {
        ILogger<HomeController> _logger;
    
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
    
        [Route("home/test")]
        public IActionResult Test()
        {
            _logger.LogInformation("Logged test information from home controller.");
    
            return Ok("Ok");
        }
    }
    


Referencer:
[1]. Kom godt i gang med ASP.NET Core (project.json)



  1. Adgang nægtet for brugeren 'root'@'localhost' (ved hjælp af adgangskode:JA) (Mysql::Fejl)

  2. mysql binær sammenligning bruger ikke indeks

  3. Sådan indsætter du JSON i en kolonnedata, hvis underforespørgsel returnerer mere end 1 række i MySQL

  4. SHOW TABLES-sætning med flere LIKE-værdier