[C#.NET] 開發夥伴的除錯利器 NLog - Database target (4)

處理 Log 時把資料存放在資料庫是一件很常用的功能,Nlog 並未限制資料庫的結構,這個結構必須要由開發人員自行定義, NLog Database target 可以讓我們輕鬆完成這件事,組態設定的語法如下:

p1

<install-command> <uninstall-command>區段可提供應用程式參數調用,請參考:http://nlog-project.org/2010/09/25/deploying-nlog-configuration.html。
<connectionStringName> 可以拿到 Web.config 的 connectionStrings 的資料。
<commandText> 是用來寫 SQL insert 語法,當然也可以寫 StoredProcedure; Parameter 當然就是新增資料庫的參數,它有兩個必填屬性,name 是資料庫的欄位,layout是 NLog 所提供的 Layout Renderers。
比較特別的是 ${appsetting} ,它是用來拿 appSettings 區段裡的資料,要從 nuget 裡另外安裝 NLog.Extended。
<target xsi:type="Database" name="NLog-SqlServer">
  <connectionStringName>elmah-sqlserver</connectionStringName>
  <commandText>
    insert into dbo.NLog (
    Application, Logged, Level, Message,
    Username,
    ServerName, Port, Url, Https,
    ServerAddress, RemoteAddress,
    Logger, CallSite, Exception
    ) values (
    @Application, @Logged, @Level, @Message,
    @Username,
    @ServerName, @Port, @Url, @Https,
    @ServerAddress, @RemoteAddress,
    @Logger, @Callsite, @Exception
    );
  </commandText>

  <parameter name="@application" layout="${appsetting:Name=ApplicationName:default=Unknown\: set AppName in appSettings}" />
  <parameter name="@logged" layout="${date}" />
  <parameter name="@level" layout="${level}" />
  <parameter name="@message" layout="${message}" />
  <parameter name="@username" layout="${identity}" />
  <parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
  <parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
  <parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
  <parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
  <parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
  <parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
  <parameter name="@logger" layout="${logger}" />
  <parameter name="@callSite" layout="${callsite}" />
  <parameter name="@exception" layout="${exception:tostring}" />
</target>

 

開始前別忘了建立資料表,個人習慣用 SQL Project 將以下 SQL 語法納入管理
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,
  [UserName] [nvarchar](250) NULL,
  [ServerName] [nvarchar](max) NULL,
  [Port] [nvarchar](max) NULL,
  [Url] [nvarchar](max) NULL,
  [Https] [bit] NULL,
  [ServerAddress] [nvarchar](100) NULL,
  [RemoteAddress] [nvarchar](100) 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]

文章出自:https://www.dotblogs.com.tw/yc421206/2015/11/23/nlog_database_target

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo