Setting up the database - MikeHanson/NLog.Mvc GitHub Wiki

NLog.Mvc provides the LoggingContext which is a DbContext instance configured to provide access to a single table in a database specified by a connection string. Although DbContext and Code First support database identification by convention I have forced the use of a connection string in configuration file.

The driver for this project was my use of DbContext and Code First in an MVC project so all my testing and the code is focused around using Code First and Migrations available in Entity Framework 4.3.*. However I see no reason why you couldn't use NLog.Mvc with an existing database where the table has been created manually, an SQL script for the table is included at the end of this page.

The following steps assume you are starting from scratch and no database or model exists. If you are already using DbContext, Code First and Migrations I am assuming you will know to skip all steps except creating the Migration that adds the table. I also assume you have added the NuGet package for NLog.Mvc, which will include the depencies for Entity Framework and NLog.

  1. Add a connection string to your web.config it must include setting MultipleActiveResultSets to true and specify a provider. The database does not need to exist. You will need something like the following:
<add name="LoggingContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=NLog.Mvc;
Integrated Security=True; MultipleActiveResultSets=True; providerName="System.Data.SqlClient" />
  1. Add a migration to create the database and log table. This involves running two or three commands in the NuGet Package Manager Console.
    1. Enable-Migrations (Optional only needed if you have never run against the selected project before)
    2. Add-Migration [Name of migration] (It is typical to have an Intialisation or similarly named migration to build your initial model and create the database if it does not exist. If you have previously run this then NlogMvc would be an appropriate name)
    3. Update-Database (This will generate and execute SQL statements to add the dbo.Log table to the database add -Verbose to have the SQL statements executed dumped to the console window)

NB: If you are trying to add NLog.Mvc to a project that has an existing DbContext instance you will need to add the LogEntryConfiguration to the DbModelBuilder.Configurations collection in some way. The simplest way to do this is to override OnModelBuilding in your existing DbContext and add the configuration something like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new LogEntryConfiguration());
}

You database should be ready now to use NLog.Mvc components.

To manually setup your database execute the following script (which is available as dbo.Log.sql in the source):

CREATE TABLE [dbo].[Log]
(
    [Id]                 [INT]             NOT NULL	 IDENTITY(1,1)  
        CONSTRAINT PK_Log PRIMARY KEY CLUSTERED,  
    [TimeStamp]          [DATETIME]	       NOT NULL,  
    [Level]              [NVARCHAR](5)     NOT NULL,  
    [Logger]             [NVARCHAR](200)   NOT NULL,  
    [Message]            [NVARCHAR](MAX)   NOT NULL,  
    [ExceptionType]      [NVARCHAR](MAX)   NULL,  
    [Operation]          [NVARCHAR](MAX)   NULL,  
    [ExceptionMessage]   [NVARCHAR](MAX)   NULL,  
    [StackTrace]         [NVARCHAR](MAX)   NULL  
)