sql >> Databáze >  >> RDS >> PostgreSQL

Logování .NET Core do PostgreSQL DB pomocí NLog

Toto fungovalo pro zápis do souboru a PostgreSQL:

  1. Nejprve jsem vytvořil webovou aplikaci ASP.NET Core (.NET Core).
  2. Poté jsem nainstaloval z NuGet:

  3. Následující soubory byly upraveny takto:

    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) Ovladač

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


Odkazy:
[1]. Začínáme s ASP.NET Core (project.json)



  1. Mechanismus, který Oracle sleduje, když bereme horké zálohy

  2. Jak nainstalovat SQLite na macOS

  3. Čím nahradit mysql_result pro MySQLi

  4. obecná chyba SQL