NLog SQL Azure set up

Anyone using NLog and want to write to SQL Azure will need the following set up;

First set up the table in SQL Azure.

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[system_logging](
    [system_logging_guid] [int] IDENTITY(1,1) NOT NULL,
    [entered_date] [datetime] NULL CONSTRAINT [DF_system_logging_entered_date]  DEFAULT (getdate()),
    [log_application] [varchar](200) NULL,
    [log_date] [varchar](100) NULL,
    [log_level] [varchar](100) NULL,
    [log_logger] [varchar](8000) NULL,
    [log_message] [varchar](8000) NULL,
    [log_machine_name] [varchar](8000) NULL,
    [log_user_name] [varchar](8000) NULL,
    [log_call_site] [varchar](8000) NULL,
    [log_thread] [varchar](100) NULL,
    [log_exception] [varchar](8000) NULL,
    [log_stacktrace] [varchar](8000) NULL,
 CONSTRAINT [PK_system_logging] PRIMARY KEY CLUSTERED 
(
    [system_logging_guid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO

SET ANSI_PADDING OFF
GO

Go to the portal and copy your connection string from ADO.net

Now copy the below test to your NLog.config file.

<targets>
    <target name="file" xsi:type="File"   layout="${longdate}|${level}|${callsite}|${logger}|${threadid}|${windows-identity:domain=false}--${message} ${exception:format=message,stacktrace:separator=*"  fileName="c:\psnet\myapplication.log" />
    <target name="database" type="Database" >
      <connectionString>
        Server=tcp:xxx.database.windows.net,1433;Database=xxx;User ID=xxx@xxx;Password={your_password_here};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;
      </connectionString>
      <commandText>
        insert into system_logging(log_date,log_level,log_logger,log_message,log_machine_name, log_user_name, log_call_site, log_thread, log_exception, log_stacktrace) values(@time_stamp, @level, @logger, @message,@machinename, @user_name, @call_site, @threadid, @log_exception, @stacktrace);
      </commandText>
      <parameter name="@time_stamp" layout="${longdate}"/>
      <parameter name="@level" layout="${level}"/>
      <parameter name="@logger" layout="${logger}"/>
      <parameter name="@message" layout="${message}"/>
      <parameter name="@machinename" layout="${machinename}"/>
      <parameter name="@user_name" layout="${windows-identity:domain=true}"/>
      <parameter name="@call_site" layout="${callsite:filename=true}"/>

      <parameter name="@threadid" layout="${threadid}"/>
      <parameter name="@log_exception" layout="${exception}"/>
      <parameter name="@stacktrace" layout="${stacktrace}"/>

    </target>
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="file"/>
    <logger name="*" minlevel="Info" appendTo="database"/>
  </rules>