Skip to content

Instantly share code, notes, and snippets.

@Toumash
Created July 8, 2019 05:33
Show Gist options
  • Save Toumash/2e8c2a1694479d104bd7bd8b5eeffb27 to your computer and use it in GitHub Desktop.
Save Toumash/2e8c2a1694479d104bd7bd8b5eeffb27 to your computer and use it in GitHub Desktop.
Nlog database (SQL Server) logging in Sharepoint environment (you cannot use nloc.config) Author: Bartosz Kusiak
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]
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment