Skip to content

Instantly share code, notes, and snippets.

@Toumash
Created July 8, 2019 05:33
Show Gist options
  • Select an option

  • Save Toumash/2e8c2a1694479d104bd7bd8b5eeffb27 to your computer and use it in GitHub Desktop.

Select an option

Save Toumash/2e8c2a1694479d104bd7bd8b5eeffb27 to your computer and use it in GitHub Desktop.

Revisions

  1. Toumash revised this gist Jul 8, 2019. No changes.
  2. Toumash created this gist Jul 8, 2019.
    12 changes: 12 additions & 0 deletions create_log_table.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    CREATE TABLE [dbo].[Log] (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Application] [nvarchar](50) NOT NULL,
    [Logged] [datetime] NOT NULL,
    [Level] [nvarchar](50) NOT NULL,
    [Message] [nvarchar](max) NOT NULL,
    [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]
    71 changes: 71 additions & 0 deletions program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Logging;
    using NLog;
    using NLog.Config;
    using NLog.Targets;
    using NLog.Web;

    namespace Sample
    {
    public class Program
    {
    public static void Main(string[] args)
    {
    var config = new LoggingConfiguration();

    DatabaseTarget dbTarget = new DatabaseTarget();
    dbTarget.Name = "db";
    dbTarget.DBProvider = "System.Data.SqlClient";
    dbTarget.ConnectionString = @"Data Source=.;Initial Catalog=database;Integrated Security=True;";
    dbTarget.CommandText =
    @"insert into dbo.Log (
    Application, Logged, Level, Message,
    Logger, CallSite, Exception
    ) values (
    @Application, @Logged, @Level, @Message,
    @Logger, @Callsite, @Exception
    );";

    dbTarget.Parameters.Add(new DatabaseParameterInfo("@application", "$AspNetCoreNlog"));
    dbTarget.Parameters.Add(new DatabaseParameterInfo("@logged", "${date}"));
    dbTarget.Parameters.Add(new DatabaseParameterInfo("@level", "${level}"));
    dbTarget.Parameters.Add(new DatabaseParameterInfo("@message", "${message}"));
    dbTarget.Parameters.Add(new DatabaseParameterInfo("@logger", "${logger}"));
    dbTarget.Parameters.Add(new DatabaseParameterInfo("@callSite", "${callsite:filename=true}"));
    dbTarget.Parameters.Add(new DatabaseParameterInfo("@exception", "${exception:tostring}"));

    config.AddTarget(dbTarget);

    var consoleTarget = new ColoredConsoleTarget("target1")
    {
    Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
    };
    config.AddTarget(consoleTarget);

    config.AddRuleForOneLevel(NLog.LogLevel.Error, dbTarget); // only errors to file
    config.AddRuleForAllLevels(consoleTarget); // all to console

    LogManager.Configuration = config;

    CreateWebHostBuilder(args).Build().Run();

    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>().
    ConfigureLogging(logging =>
    {
    logging.ClearProviders();
    logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
    })
    .UseNLog();
    }
    }