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.
- <targets/> define the target/output of the log
- Type-types of targets-such as "File", "Database", "Mail". If you use namespaces, this property will be named Xsi:type.
- <rules/>-Define routing rules for logs
- <extensions/>-load nlog extension from *.dll
- <include/>-Import External configuration file
- <variable/>-Assigning a value to a configuration variable
ASP. NET Core NLog output logs to database and add layoutrenderer support