ASP. NET Core NLog output logs to database and add layoutrenderer support

Source: Internet
Author: User

   

Before this is intended to use Apache log4net, but found that its Ado Netappender method no longer exists, can not use the configuration file directly output to the database , so I switched to the nlog framework.

First, add Nlog to the project
Install NLog NLog.Extensions.Logging, NLog.Web.AspNetCore with NuGet
Ii. Add the Nlog.config profile to the Nlog.config configuration item

<?XML version="1.0"encoding="Utf-8"?>
<Nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Autoreload="true"
Internalloglevel="Trace"
Internallogfile="Internal-nlog.txt">
<!-- define various log targets -
<Targets>
<!--write logs to file -
<Target Xsi:type="File"name="Allfile"FileName="${var:configdir}\nlog-all.log "
Layout= "${longdate}|${event-properties:item=eventid.id}|${logger}|${uppercase:${level}}|${message} ${exception} "/>


    < target Span style= "color: #ff0000;" >xsi:type = "file " name = "ownfile-web "filename =" ${var: Configdir}\nlog-own.log "
             layout=" ${ longdate}|${event-properties:item=eventid.id}|${logger}|${uppercase:${level}}|  ${message} ${exception} "/>

< Target xsi:type= "Null" name= "blackhole" />

<Target name="Database"Xsi:type="Database">
<connectionString>${var:connectionstring}</connectionString>
<CommandText>
INSERT into [dbo]. [System_sqllog]
([Sqllogid]
, [Createuserid]
, [Createusercode]
, [Createusername]
, [Createtime]
, [Operatesql]
, [EndDateTime]
, [ElapsedTime]
, [Parameter])
VALUES
(@SqlLogId
, @CreateUserId
, @CreateUserCode
, @CreateUserName
, @CreateTime
, @OperateSql
, @EndDateTime
, @ElapsedTime
, @Parameter);
</CommandText>
<parameter name="@Sqllogid"Layout="${event-context:item=sqllogid}"/>
<parameter name="@Createuserid"Layout="${event-context:item=createuserid} " />
<parameter name="@Createusercode"Layout="${event-context:item=createusercode} "/>
<parameter name="@Createusername"Layout="${event-context:item=createusername} "/>
<parameter name="@Createtime"Layout="${event-context:item=createtime} "/>
<parameter name="@Operatesql"Layout="${event-context:item=operatesql} " />
<parameter name="@EndDateTime"Layout="${event-context:item=enddatetime} " />
<parameter name="@ElapsedTime"Layout="${event-context:item=elapsedtime} " />
<parameter name="@Parameter"Layout="${event-context:item=parameter} " />
</Target>
</Targets>
<rules>
<!--All logs, including from Microsoft -
<Logger name="*"MinLevel="Trace"WriteTo="Allfile"/>

<!--Skip Microsoft Logs and so logs only own logs-
<Logger name="microsoft.*" MinLevel="Trace"WriteTo="blackhole"Final="true"/>
<Logger name="Sqllogtodatabase"MinLevel="Debug"WriteTo="Database"/>
<Logger name="*"MinLevel="Trace"WriteTo="Ownfile-web"/>
</rules>
</Nlog>

    • Nlog node If set Internalloglevel,internallogfile can view the internal information of the Nlog output log, and can check the configuration file errors inside and so on.
    • Database target can specify CONNECTIONSTRING,SQL statements, SQL parameters, etc.
Third, add Nlog to. Net Core Four, execute

public virtual void Writelog () {
Logger ILog = Logmanager.getcurrentclasslogger ();
if (ilog.isinfoenabled) {
Logeventinfo ei = new Logeventinfo (NLog.LogLevel.Info, "", "");
Ei. properties["Sqllogid"] = Combutil.newcomb ();
Ilog.info (EI);
}
}

This allows you to add the defined values to the database. V. Layoutrenderer application according to the above operation and can not meet my current framework of the application, I need to direct the object, but directly ilog.info (T), and will not get the value, he will get to the null value.    To do this, you need to customize the layoutrenderer. We can see that he is a generic method, so it is possible to insert objects inside. Let me tell you how to rewrite layoutrenderer. Directly on the codeAppend will return a data to the current caller. Then we'll modify the configuration file Nlog.config. and we also need to load this assemblyThis will insert the corresponding value into our database.
    1. <targets/> define the target/output of the log
      1. Type-types of targets-such as "File", "Database", "Mail". If you use namespaces, this property will be named Xsi:type.
    2. <rules/>-Define routing rules for logs
    3. <extensions/>-load nlog extension from *.dll
    4. <include/>-Import External configuration file
    5. <variable/>-Assigning a value to a configuration variable

ASP. NET Core NLog output logs to database and add layoutrenderer support

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.