您可以使用My.Application.Log
和My.Log
对象来记录有关应用程序中发生事件的信息。 此示例演示如何使用 My.Application.Log.WriteEntry
该方法将跟踪信息记录到日志文件。
添加和配置文件日志侦听器
在 解决方案资源管理器 中右键单击 app.config,然后选择“ 打开”。
- 或 -
如果其中没有 app.config 文件:
在 “项目” 菜单上,选择“ 添加新项”。
在“ 添加新项 ”对话框中,选择 “应用程序配置文件”。
单击 添加。
在应用程序配置文件中找到该
<listeners>
部分。<listeners> 部分位于名称属性为“DefaultSource”的 <source> 部分中,后者嵌套在 <system.diagnostics> 部分中,该部分又嵌套在顶级 <configuration> 部分下。
将此元素添加到该
<listeners>
部分:<add name="FileLogListener" />
找到
<sharedListeners>
部分,该部分位于<system.diagnostics>
部分中,后者嵌套在顶级<configuration>
部分之下。将此元素添加到该
<sharedListeners>
部分:<add name="FileLogListener" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" initializeData="FileLogListenerWriter" location="Custom" customlocation="c:\temp\" />
将
customlocation
属性的值更改为日志目录。注释
若要设置侦听器属性的值,请使用与该属性同名的属性,并确保名称中的所有字母均为小写。 例如,
location
和customlocation
属性设置 Location 和 CustomLocation 属性的值。
将事件信息写入文件日志
请使用My.Application.Log.WriteEntry
或My.Application.Log.WriteException
方法将信息写入文件日志。 有关详细信息,请参阅如何:编写日志消息和如何:记录异常。
为程序集配置文件日志侦听器后,它将接收从该程序集写入的所有消息 My.Application.Log
。