NLog 通过http保存日志

简介

NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码。 NLog是一个简单灵活的.NET日志记录类库。通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的(contextual information)调试诊断信息,根据喜好配置其表现样式之后发送到一个或多个输出目标(target)中。

NLog的API非常类似于log4net,且配置方式非常简单。NLog使用路由表(routing table)进行配置,但log4net却使用层次性的appender配置,这样就让NLog的配置文件非常容易阅读,并便于今后维护。 NLog遵从BSD license,即允许商业应用且完全开放源代码。任何人都可以免费使用并对其进行测试,然后通过邮件列表反馈问题以及建议。

NLog支持.NET、C/C++以及COM interop API,因此我们的程序、组件、包括用C++/COM 编写的遗留模块都可以通过同一个路由引擎将信息发送至NLog中。

代码实现

1、自定义变量

<variable name="accessToken" value="UKncvUQRoPWx8lCvwED105GoWYikIGrOPzGpJOMQMCB" /><variable name="notifyApiUrl" value="http://localhost:8088/LogCollection" /><variable name="typesite" value="YuanFeng.LegalDoc.Api" />

2、配置输出

<target type="WebService" encoding="utf-8" name="lineNotify" url="${notifyApiUrl}" protocol="HttpPost"><parameter name="accessToken" type="System.String" layout="${accessToken}" /><parameter name="localip" type="System.String" layout="${local-ip}" /><parameter name="level" type="System.String" layout="${level}" /><parameter name="typesite" type="System.String" layout="${typesite}" /><parameter name="message" type="System.String" layout="YuanFeng.LegalDoc.Api - ${local-ip}|${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /></target>

3、新建log项目,接收日志

[HttpPost]public bool Post([FromForm] Logarg request){if (request.accessToken != "UKncvUQRoPWx8lCvwED105GoWYikIGrOPzGpJOMQMCB"){return false;}if (request.level == "Error")logger.LogError(request.message + "{localipstr} {typesite}", request.localip, request.typesite);if (request.level == "Info")logger.LogInformation(request.message + "{localipstr} {typesite}", request.localip, request.typesite);return true;}

4、输出日志到文件

<?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"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"autoReload="true"throwExceptions="false"internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"><variable name="myvar" value="myvalue" /><targets><target xsi:type="File" name="f" fileName="${basedir}/logs/${event-properties:item=localipstr}/${event-properties:item=typesite}/${shortdate}/${level}.log"layout="${longdate} ${uppercase:${level}} ${message}"maxArchiveFiles="999"archiveAboveSize="10485760" /></targets><rules><logger name="*" minlevel="Info" writeTo="f" /></rules>
</nlog>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280231.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

嵌套映射

1. 多对一嵌套查询映射使用案例 package com.zixue.dao;import com.zixue.annotation.MyBatisRepository; import com.zixue.entity.Emp;/*** 员工表的DAO组件* */ MyBatisRepository public interface EmpDao {void save(Emp emp);Emp findById(int id);Emp findById2(int id)…

gopro dataset_如何将GoPro安装到DSLR相机

gopro datasetIf you have a DSLR camera with a hot shoe, it’s easy to attach various flashes and other accessories right to your camera. But with a couple of cheap attachments on hand, you can mount your GoPro to your DSLR camera as well. 如果您的DSLR相机带…

音频 m4a 转 wav

背景最近做智能家居&#xff0c;需要用到一些应答词 需要自己录制。但是在mac下面通过 QuickTime 录制的是 m4a格式。但是应答词需要 wav格式。所以就需要转化了解决方法# sox 不行&#xff0c; ffmpeg 很麻烦&#xff0c;用 avconv 很简单。安装 如果没有就安装 # apt-get ins…

jQuery已经过时了,还需要学吗?

说起jQuery&#xff0c;很多刚参加工作的程序员都没用过&#xff0c;甚至没听过。曾几何时jQuery可是秒杀一切Js库&#xff0c;大有一统江山的情况&#xff0c;可是在顶峰的时候&#xff0c;瞬间被Vue、React、Angela三大框架斩于马下。从百度指数&#xff0c;我们也看出在2015…

Bootstrap01

Bootstrap01内容概要 一.使用Bootstrap的步骤 1.下载Bootstrap类库,包含三个部分,fonts,css,Bootstrap 2.导入项目中,在头部引入JQ,css和Bootstrap 注意:JQ要引入在Bootstrap前面! 3.使用css样式时,全部使用class属性 二.全局CSS概要 1.仅支持H5文档格式 2.移动设备优先,需要在…

ios raise_如何在iOS 10中关闭“ Raise to Wake”

ios raiseRaise to Wake is a new Lock screen feature available in iOS 10. It allows you to wake your phone’s screen simply by picking up your phone. This feature is on by default, but if you’d rather not use it, it’s simple to turn off. “唤醒”是iOS 10中…

资源调度器调研

2019独角兽企业重金招聘Python工程师标准>>> 场景描述&#xff1a; 异步触发和Crontab触发 YARN(Yet Another Resource Negotiator)Hadoop 资源管理器 主要构成&#xff1a; RM(ResourceManager)是一个全局的资源管理器&#xff0c;负责整个系统的资源管理和分配。…

WPF-19 IValueConverter接口

我们先来看看微软官方给出的定语&#xff1a;提供将自定义逻辑应用于绑定的方法&#xff0c;我们来看一下该接口的定义&#xff0c;Convert提供了将数据源到UI的格式化&#xff0c;ConvertBack表示反向namespace System.Windows.Data {//// Summary:// Provides a way to a…

JVM学习记录-类加载的过程

类的整个生命周期的7个阶段是&#xff1a;加载&#xff08;Loading&#xff09;、验证(Verification)、准备(Preparation)、解析(Resolution)、初始化(Initialization)、使用(Using)、卸载(Unloading)。 类加载的全过程主要包括&#xff1a;加载、验证、准备、解析、初始化这5个…

使用 Azure CLI 将 IaaS 资源从经典部署模型迁移到 Azure Resource Manager 部署模型

以下步骤演示如何使用 Azure 命令行接口 (CLI) 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure Resource Manager 部署模型。 本文中的操作需要 Azure CLI。 Note 此处描述的所有操作都是幂等的。 如果你遇到功能不受支持或配置错误以外的问题&#xff0c;建议你…

c++的进制转换函数

https://blog.csdn.net/u010003835/article/details/47665847https://blog.csdn.net/vir_lee/article/details/80645066strtol函数&#xff1a;用于由十进制转化到2~36的其他进制。函数原型为&#xff1a;long int strtol(const char *nptr,char **endptr,int base); 还应该注意…

黑苹果不能imessage_如何修复iMessage在iOS 10中不显示消息效果

黑苹果不能imessageiMessage got a huge update in iOS 10, adding things like third-party app integration, rich links, and a number of fun graphical effects for messages. If you’re seeing messages that say something like “(sent with Invisible Ink)” instead…

MyBatisPlus怎么忽略映射字段

TableField(exist false)&#xff1a;表示该属性不为数据库表字段&#xff0c;但又是必须使用的。 TableField(exist true)&#xff1a;表示该属性为数据库表字段。 Mybatis-Plus 插件有这个功能&#xff0c;可以看一下 TableName&#xff1a;数据库表相关 TableId&#xff1…

从技术总监到开源社区运营:过去两年,我都做了点啥?

这是头哥侃码的第267篇原创今天&#xff0c;这是我离开前公司的第 7 天。相信有不少吃瓜群众都很好奇&#xff0c;你这些天都在干啥&#xff1f;是不是蓬莱乐逍遥&#xff0c;过上了那悠闲的神仙日子&#xff1f;还是趁着疫情管控逐渐放开&#xff0c;和家人一起去深山老林里吸…

查看模拟器使用端口_为什么我们仍然使用模拟音频端口?

查看模拟器使用端口When leaks about what the chassis of the iPhone 7 might look like hit headlines earlier this week, technology columnists and industry analysts jumped on the chance to report that Apple’s next device may finally ditch its 3.5mm audio port…

ServletContextListener在Springboot中的使用

ServletContextListener是servlet容器中的一个API接口, 它用来监听ServletContext的生命周期&#xff0c;也就是相当于用来监听Web应用的生命周期。今天我们就来说说如何在Springboot 1.5.2这个轻量型框架中如何使用它。 其实配置ServletContextListener与其它Filter, Listener…

《ASP.NET Core 6框架揭秘》实例演示[34]:缓存整个响应内容

我们利用ASP.NET开发的大部分API都是为了对外提供资源&#xff0c;对于不易变化的资源内容&#xff0c;针对某个维度对其实施缓存可以很好地提供应用的性能。《内存缓存与分布式缓存的使用》介绍的两种缓存框架&#xff08;本地内存缓存和分布式缓存&#xff09;为我们提供了简…

常见端口介绍

Win常用端口 TCP端口&#xff08;静态端口&#xff09;TCP 0 ReservedTCP 1TCP Port Service MultiplexerTCP 2DeathTCP 5Remote Job Entry,yoyoTCP 7EchoTCP 11SkunTCP 12BomberTCP 16SkunTCP 17SkunTCP 18消息传输协议&#xff0c;skunTCP 19SkunTCP 20FTP Data,Amanda TCP 2…

如何更改Windows 10锁定屏幕超时

By default, Windows 10’s lock screen times out and switches off your monitor after one minute. If you’d like it to stick around longer than that–say, if you have background picture you like looking at or you enjoy having Cortana handy–there’s a simple…

ios 开发账号 退出协作_如何在iOS 10中的Notes上进行协作

ios 开发账号 退出协作iOS’ Notes app provides a convenient way to remember the great ideas you come up with and all the things you have to do. The app has evolved over the years, and iOS 10 adds even more features–including collaboration. iOS的Notes应用程…