小于 1 分钟
NLog 日志写入 mongodb
- 1、安装包
NLog.Mongo
- 2、修改
NLog.config
在 targents 节点中添加以下代码
<target xsi:type="Mongo"
name="mongo"
databaseName="logging"
collectionName="adminLogs"
connectionString="mongodb://127.0.0.1:27017"
cappedCollectionSize="5368709120"
IncludeDefaults="false">
<Field name="CreateTime" layout="${longdate}" bsonType="DateTime" />
<Field name="Level" layout="${uppercase:${level}}" />
<Field name="Logger" layout="${logger}"/>
<Field name="Message" layout="${message}" />
<Field name="Exception" layout="${exception:format=tostring}" />
<!--<Field name="CallSite" layout="${callsite}"/>-->
<Field name="UserIP" layout="${aspnet-request-iP:CheckForwardedForHeader=true}"/>
<Field name="RequestPath" layout="${aspnet-request-url}"/>
<Field name="UserAgent" layout="${aspnet-request-useragent}"/>
<Field name="Method" layout="${aspnet-mvc-controller}.${aspnet-mvc-action}()"/>
<Field name="Tid" layout="${aspnet-traceidentifier}"></Field>
<!--<Field name="Hostname" layout="${hostname} "/>-->
<Field name="RequestMethod" layout="${aspnet-request-method}"></Field>
<Field name="Headers" layout="${aspnet-request-headers}"></Field>
<!--自定义-->
<Field name="Status" layout="${event-properties:item=status}"></Field>
<Field name="Device" layout="${event-properties:item=device}"></Field>
<Field name="Title" layout="${event-properties:item=title}"></Field>
<Field name="RequestParam" layout="${event-properties:item=requestParam}"></Field>
<Field name="ResponseResult" layout="${event-properties:item=jsonResult}"></Field>
<!--请求用时-->
<Field name="Elapsed" layout="${event-properties:item=elapsed}"></Field>
<Field name="User" layout="${aspnet-request-headers:HeaderNames=useridx}"></Field>
</target>