You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 配置文件概述
## 配置文件智能提示
+ 方法一: 引入Nuget包:NLog.config 和 NLog.Schema
+ 方法二: 下载NLog.xsd文件, 并配置
> 下载提示文件 https://nlog-project.org/schemas/NLog.xsd 并放入项目根目录
> 编辑nlog.config文件, 在nlog根节点中添加属性: xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
## 配置文件位置
### 自动匹配
在程序启动时, NLog会在各种文件中搜索其配置(如下所述)。它加载找到的第一个nlog配置文件: 按下述默认位置, 依次查找nlog配置, 找到第一个, 搜索结束, 使用配置; 全部位置搜索完, 仍然找不到配置, 则NLog失败。
对于独立的* .exe应用程序, 文件搜索如下:
+ 标准应用程序配置文件( 通常为applicationname.exe.config)
+ 应用程序目录中的applicationname.exe.nlog
+ 应用程序目录中的NLog.config( 名称敏感; 使用docker dotnet core)
+ NLog.dll所在目录中的NLog.dll.nlog( 仅当GAC中未安装NLog时)
对于ASP.NET应用程序, 文件搜索如下:
+ 标准Web应用程序文件web.config
+ web.nlog与web.config位于同一目录
+ 应用程序目录中的NLog.config
+ NLog.dll所在目录中的NLog.dll.nlog( 仅当GAC中未安装NLog时)
### 显式(手动)指定匹配
+ 加载配置文件: 指定Nlog.config文件
```csharp
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("nlog.config");
```
+ 加载配置文件:从指定字符串
```csharp
var xmlStream = new System.IO.StringReader("< nlog > *****</ nlog > ");
var xmlReader = System.Xml.XmlReader.Create(xmlStream);
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(xmlReader, null);
```
+ 加载配置文件: Xamarin 资源文件
+ 加载配置文件: Xamarin Android, 自动扫描 assets文件夹中的 NLog.config
```csharp
LogManager.Configuration = new XmlLoggingConfiguration("assets/someothername.config");
```
## 配置文件布局
NLog配置的格式为XML, 并且可以嵌入Visual Studio项目配置文件( app.config或web.config) 中, 也可以是独立的XML文件。
+ 嵌入到项目配置文件中
```xml
< configuration >
< configSections >
<!--添加Nlog配置节-->
< section name = "nlog" type = "NLog.Config.ConfigSectionHandler, NLog" />
</ configSections >
...
<!--NLog配置-->
< nlog >
...
</ nlog >
</ configuration >
```
+ 独立XML文件: **NLog.config**
```xml
< nlog xmlns = "http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" >
...
</ nlog >
```
## 顶级元素
nlog配置有以下顶级元素,其中targets和rules在任何配置中都是必需的; 其他为可选项, 在高级方案中很有用。
+ targets :定义日志目标/输出
+ rules :定义日志路由规则
+ extensions :从* .dll文件加载NLog扩展程序集
+ include :包括外部配置文件
+ variable :设置配置变量
最简单的配置: 一个target 和 一个rule