nlog

.Net项目中NLog的配置与使用

匿名 (未验证) 提交于 2019-12-02 23:48:02
引言:   因为之前在项目开发中一直都是使用的Log4Net作为项目的日志记录框架,最近忽然感觉对它已经有点腻了,所以尝试着使用了NLog作为新项目的日志记录框架(当然作为一名有志向的攻城狮永远都不能只局限于眼前的技术,要不断的使用和学习新的技术)。当然serilog也是一个不错的日志记录框架哟,不过今天主要还是要讲述的是NLog在项目中的配置和使用。 NLog框架源码: https://github.com/NLog/NLog 一、导入NLog NuGet PackAge: 注意:在这里我是专门新建了一个NLog.config 配置文件用来进行独立配置,当然你也可以在web.config中完成相应的配置!! NLog详细配置文件信息,请查看官网说明: https://github.com/nlog/nlog/wiki/configuration-file 可参考晓晨大佬的NLog配置: https://www.cnblogs.com/stulzq/p/8504860.html a、我的NLog.config 配置代码: <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001

控制台程序秒变Windows服务(Topshelf)

匿名 (未验证) 提交于 2019-12-02 23:47:01
项目中有些时候需要写服务,一般我们都是先创建控制台程序,测试,运行,成功之后再创建windows服务程序,这样好麻烦啊,有没有简单的控制台程序直接变成Widnows服务,经过查找,找到了Topshelf。Topshelf是一个托管使用.NET框架编写的服务的框架,简化了服务的创建,允许开发人员创建一个简单的控制台应用程序,可以使用Topshelf作为服务安装。 Topshelf介绍 Topshelf是一个托管使用.NET框架编写的服务的框架。简化了服务的创建,允许开发人员创建一个简单的控制台应用程序,可以使用Topshelf作为服务安装。原因很简单:调试控制台应用程序比使用服务要容易得多。一旦应用程序经过测试并可以投入生产,Topshelf便可以轻松地将应用程序作为服务进行安装。这是一个开源的项目, 项目地址 ,Nuget上可以搜到响应的库。 Topshelf使用 1.创建控制台程序 2.安装Topshelf,在Nuget上搜下 3.安装NLog、NLog.Config,目的是为了看日志,可以清楚的知道服务在运行,可以不要 NLog.Config简单配置 <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http:

NLog GetCurrentClassLogger() NullReferenceException using StructureMap (Full Trust)

只愿长相守 提交于 2019-12-02 23:34:14
It seems like NLog can't use reflection for GetCurrentClassLogger() , even though my MVC 3 app is deployed in a Full Trust environment on IIS7. I'm using StructureMap 2.6.1 and the problem seems to appear sporadically between deploys. I can't figure out why, though I don't think StructureMap is causing it. Bootstrapper class: public static class Bootstrapper { public static void ConfigureStructureMap() { ObjectFactory.Initialize(Init); } private static void Init(IInitializationExpression x) { x.AddRegistry(new DBServiceRegistry()); x.AddRegistry(new MyRegistry()); } } Registry class: public

NLog日志框架使用探究

匿名 (未验证) 提交于 2019-12-02 23:03:14
NLog日志框架使用探究 前言 为什么是NLog? 目的 配置 基本配置 日志等级 输出例子 目标 文件输出 Json格式保存 多目标 参数 规则 日志分发 日志收集 参考文档 日志是每个程序的基本模块。本文是为了探究如何通过NLog方便及记录日志并通过Log4View工具收集日志统一查看。 下载量NLog和Log4Net差不多,这两个日志模块是.Net平台使用最多的两大日志模块。 Log4Net上次更新已经是17年3月 NLog更新的比较频繁,开发者比较活跃,有问题的话修复更及时。 NLog是适用于各种.net平台(包括.net standard)的灵活而免费的日志记录平台。通过NLog, 可以轻松地写入多个目标。(数据库、文件、控制台), 并动态更改日志记录配置。 NLog支持结构化和传统日志记录。NLog的特点: 高性能、易于使用、易于扩展和灵活配置。 本文为了探究NLog的使用方式,以及如何通过NLog将日志统一收集查看并管理。 NLog可以通过配置方式轻松的记录不同等级,不同结构的日志。 通过Nuget获取NLog库包 Install-Package NLog -Version 4.5.11 下载完后会自动在程序下加入默认的NLog配置 <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog

Nlog、Log4Net 的一个小扩展(增加自定义LEVEL)

匿名 (未验证) 提交于 2019-12-02 22:10:10
因公司ELK监控分析日志的需要,需要区分进程运行状态日志以及错误日志,以便能够根据日志级别(level)进行不同策略的预警,而现有的Nlog、Log4Net都没有Process这样的level,故针对这两个日志框架做了一些扩展,实现了自定义PROCESS LEVEL,因代码不多,故直接贴代码,有疑问的或好的建议可以下方评论留言交流,谢谢! NlogExtend.cs:(Nlog的扩展)文件代码内容如下: public static class NlogExtend { public static void Process(this ILogger logger, string message = "RUNNING") { var logEventInfo = new LogEventInfo(LogLevel.Trace, logger.Name, message); logEventInfo.Properties["custLevel"] = Tuple.Create(9, "Process"); logger.Log(logEventInfo); } } [LayoutRenderer("levelx")] public class LevelExLayoutRenderer : LevelLayoutRenderer { protected override void

netcore的NLog使用小记

匿名 (未验证) 提交于 2019-12-02 22:06:11
1. 启动应用程序日志配置 修改 Program.cs ,在WebHostBuilder构建时配置日志 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>   WebHost.CreateDefaultBuilder(args)   .UseStartup<Startup>()   .ConfigureLogging(logging =>   {     logging.ClearProviders();     logging.SetMinimumLevel(LogLevel.Information);     logging.AddConsole();   }).UseNLog(); 其中,UseNLog是拓展方法,需要引入NLog.Web.AspNetCore 2. 新增配置文件nlog.config <?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">   < targets>     <

C# 使用NLog记录日志入门操作

匿名 (未验证) 提交于 2019-12-02 22:06:11
1、首先用VS2010创建命令行工程NLogDemo 2、在程序包管理器控制台中输入:Install-Package NLog -Version 4.4.12   这句是怎么来的,要是你用过nuget包管理工具,那就可以跳过这里的说明了。   要使用nuget添加NLog包到项目中。请看下图。 然后在程序包管理工具控制台下输入:Install-Package NLog -Version 4.4.12 再看看Install-Package NLog -Version 4.4.12这个怎么找。 打开百度搜索:Nuget 然后在Nuget官网上搜索栏输入:Nlog 回车 选择第一项Nlog 至于为什么选择这个版本,因为这个版本下载的次数多。嘿嘿,没办法,随大流。当然还要看这个版本包的依赖项 这个包的没有什么特殊依赖项,所以可以使用。然后拷贝 NLog 包添加完之后,还要添加 NLog.config(4.4.12)(Install-Package NLog.Config -Version 4.4.12) 这个包,按道理 NLog 和 NLog.config 应该一起的, 突然从某个版本开始分开了,要单独添加。具体情况可以去官网看介绍: https://nlog-project.org/ 添加 NLog.config 的方法跟上面添加 NLog 的方法一样。 3、简单封装 Log  

Asp.Net Core实战(干货)

匿名 (未验证) 提交于 2019-12-02 22:06:11
使用.NET Core,团队可以更容易专注的在.net core上工作。比如核心类库(如System.Collections)的更改仍然需要与.NET Framework相同的活力,但是ASP.NET Core或Entity Framework Core可以更轻松地进行实质性更改,而不受向后兼容性的限制。.NET Core借鉴了.NET Framework的最佳实践,并将软件工程的最新进展结合在一起。 寒暄、扯淡已经完毕,,,下面是我最近时间对.Net Core整理的相关知识,觉得这些在项目中是最基础且最会应用到的,,,,不喜欢扯理论,直接撸码: 1、浅谈Startup类 2、自定义路由 3、跨域设置 4、自定义读取配置文件信息 5、程序集批量依赖注入 6、使用NLog写入文件日志 7、使用NLog写入数据库日志 8、Nlog标签解读 -> configureServices ->configure ConfigureServices方法:主要用于服务配置,比如依赖注入(DI)的配置,使用时该方法必须在Configure方法之前 Configure方法:用于应用程序响应HTTP请求,通过向IApplicationBuilder实例添加中间件组件来配置请求管道 在Startup类的Configure方法配置 public void Configure

How do I configure NLog to write to a database?

强颜欢笑 提交于 2019-12-02 20:23:31
I'm trying to get NLog to write to a database, however with my current code it throws an exception when I attempt to debug, the exception is: The type initializer for 'NotifyIcon.Program' threw an exception. my NLog configuration file code is below, as this seems to be causing the issue as it's the only code I've changed. <?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"> <!-- See http://nlog-project.org/wiki/Configuration_file for information on customizing logging rules

How do I email errors logged with NLog? [closed]

醉酒当歌 提交于 2019-12-02 20:11:02
I am using NLog for the first time, i figured out how to write to a text file, but now i want to send an email to myself. am making the assumption that when you supply SMTP credentials to NLog. The assembly calls the System.Net.Mail namespace and handles sending an email. If this is wrong please tell me. And if you have done this before i would appreciate any information on what it took you to accomplish send emails. Below is my configuration. <?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"