前言
本文主要介绍了ASP.NET Core 开发-Logging 使用NLog 写日志文件的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧
NLog 可以适用于 .NET Core 和 ASP.NET Core 。
ASP.NET Core已经内置了日志支持,可以轻松输出到控制台。
学习Logging 组件的相关使用,使用NLog 将日志写入到文件记录。
Logging 使用
新建一个 ASP.NET Core 项目,为了方便,我选择Web 应用程序,改身份验证 改为 不进行身份验证。
新建好以后,会自动引用好对应的 类库。这样我们就可以直接使用 Logger。
Logger 在 Controller的使用
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("你访问了首页");
_logger.LogWarning("警告信息");
_logger.LogError("错误信息");
return View();
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Error()
{
return View();
}
}
|
使用DI 直接可以使用对象。
你会发现日志信息输出来的是乱码,这里我们要指定输出格式。
需要添加 System.Text.Encoding.CodePages 引用
|
1
|
Install-Package System.Text.Encoding.CodePages -Pre
|
然后在 Startup.cs —> Configure
|
1
2
3
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
这样在控制台显示就不会出现乱码。
日志级别:Trace -》Debug-》 Information -》Warning-》 Error-》 Critical
级别包含范围由大到小 ,如 Trace 就包含了所有信息。
NLog 使用
1.添加引用。
|
1
|
Install-Package NLog.Extensions.Logging -Pre
|
2.添加nlog.config 文件在项目里。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?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="Warn"
internalLogFile="internal-nlog.txt">
<!-- define various log targets -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="nlog-all-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="File" name="ownFile-web" fileName="nlog-own-${shortdate}.log"
layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
|
3.在 Startup.cs -》 Configure
|
1
2
3
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();//添加NLog
|
运行程序,你就会发现,项目下多了两个文件,证明成功执行。
这里 nlog-all-*.log 是记录所有日志,nlog-own-*.log 记录跳过Microsoft 开头的类库输出的相关信息,剩下的信息。
4.发布(dotnet publish)注意事项
在 project.json 的 publishOptions节点 加入 nlog.config
|
1
2
3
4
5
6
7
8
9
|
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config",
"nlog.config"//加上nlog配置文件
]
},
|
GitHub :https://github.com/linezero/Blog/tree/master/NETCoreLogging
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
原文链接:http://www.cnblogs.com/linezero/p/Logging.html
相关文章
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-06-04 78
-
2025-05-29 70
-
2025-05-29 47
-
2025-05-25 105
-
2025-05-25 90


