TraceLog in VB.NET

本文介绍了如何利用.NET Framework提供的跟踪和调试工具,通过多种方式记录应用程序的运行情况,包括使用预编译常量、布尔开关及跟踪开关来控制跟踪消息的级别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tracing with .NET
The .NET Framework includes classes and data types that make it easy to log trace messages—the logging infrastructure is right there for you. Figure 1 lists the .NET Framework classes that you use for tracing. The classes are part of the System.Diagnostics namespace. The Trace class exposes numerous static (shared) methods. The Write methods let you log a specific message, while Assert lets you log a message if a specified condition is false. The Fail method is similar to an Assert; the condition is always false, so the Fail method always logs the specified message.
The Trace class outputs your messages to objects in its Listeners collection. The framework includes EventLogTraceListener, which writes to an event log; TextWriterTraceListener, which writes to a text file; and DefaultTraceListener, an instance of which is added by default to the Trace class's Listeners collection. The DefaultTraceListener class's Write and WriteLine methods emit the message to the attached debugger, which may or may not display the message.
You can implement your own trace listener. You might want to receive trace output from your application running on a remote machine behind a firewall. In that case, you can write a trace listener that sends trace messages to your server over HTTP requests. Of course, this would significantly degrade your application's performance, but only while tracing is enabled.
While the Write and WriteLine methods provide the basic functionality you need to log trace messages, WriteIf and WriteLineIf let you control whether a message is to be logged based on the application's trace level setting. There are three ways that you can control tracing. You can define the compilation constant TRACE and set it to True, telling the compiler to keep your tracing code (for example, calls to Trace.WriteLine) in the compiled binary. You can do this using the compiler command-line parameter /d:TRACE=True or by checking the Define TRACE constant box in the Visual Studio project properties dialog, as shown in Figure 2. Setting this constant to False causes the compiler to strip off all tracing code from the compiled binary.

Figure 2 Enabling Tracing in Visual Studio .NET
Figure 2 Enabling Tracing in Visual Studio .NET

Figure 3 shows the Microsoft intermediate language (MSIL) output for a method that was compiled with TRACE=True. You'll notice there's a call to System.Diagnostics.Trace::WriteLineIf. Compare that to Figure 4, which is the MSIL output for the same method compiled with TRACE=False, which causes all calls to the Trace class methods to be removed by the compiler. The TRACE compilation constant is an effective way to make debug or troubleshooting versions of your programs with lots of tracing code; to create a more efficient production version, you just have to change a single constant. What's more, you don't have to enclose all tracing calls in #If statements as you would in Visual Basic 6.0.

BooleanSwitch
The second way you can control tracing during execution of an app without having to recompile is using the BooleanSwitch class. You do this by specifying whether tracing is on or off in a registry value or in an environment variable. BooleanSwitch's constructor accepts two parameters: the switch's name and its description.
Private Shared BITraceBooleanSwitch As New _ 
BooleanSwitch("BITraceControl", _ 
 "Batch Importer Trace Control")
The switch is off by default. To turn it on, either set a registry value or an environment variable. The registry value for this particular switch is:
HKLM/Software/SOFTWARE/Microsoft/COMPlus/Switches/BITraceControl
Note that the value name matches the switch's name that you specify when instantiating the switch object. You set this value to 0 or 1 to turn the switch off or on, respectively. You can also set the environment variable _switch _BITraceControl to 0 or 1.
This switch has no effect until you use it in your own If...Then statement or with Trace.WriteLineIf. For example:
Trace.WriteLineIf( _ 
  BITraceBooleanSwitch.Enabled, _ 
  "this means the switch is" &_   
  "turned on")
This line will log the message "this means the switch is turned on" only if the switch is turned on. Otherwise, it'll simply ignore it. The switch value is read once during execution (an optimization to avoid repetitive reading from the registry), so if you change the registry setting, you'll need to restart the application for the change to take effect.

TraceSwitch
In most cases you'll have various types of trace messages like error, warning, and information messages. Use TraceSwitch to control which message levels are logged. Like the BooleanSwitch, you specify TraceSwitch's name when you instantiate it and you set its value in the registry or an environment variable. The possible values for a TraceSwitch are 0 (off), 1 (errors), 2 (warnings), 3 (information), and 4 (verbose).
TraceSwitch has four properties: TraceError, TraceInfo, TraceWarning, and TraceVerbose. Each of these properties returns True if messages of that type are allowed to be logged. If you set the tracing level to 3 (information) then TraceError, TraceWarning, and TraceInfo will all return True, while Trace Verbose will return False. You can use TraceSwitch with Trace.WriteLineIf like this:
Trace.WriteLineIf(BITraceLevel.TraceInfo, _ 
    "This is my informational message")

BITraceLevel is an object that is instantiated from TraceSwitch. Figure 5 demonstrates using Trace, EventLogListener, and TraceSwitch to write messages to the event log. Messages will appear in the application log on the local computer with the message source set to BatchImporter.

TraceTrack: http://msdn.microsoft.com/msdnmag/issues/01/07/vbnet/

 

内容概要:本文详细介绍了在Linux环境下进行C++开发所需掌握的内容,以一个Web多人聊天项目为例,涵盖从开发环境搭建到项目部署的全过程。首先推荐了一个项目地址,该项目支持HTTP请求、Websocket、多房间和多人聊天、MySQL用户信息存储、Redis缓存、json序列化等功能,并建议扩展功能如基于Reactor模型构建HTTP/Websocket服务、仿写MySQL/Redis连接池等。接着介绍了开发环境,包括Ubuntu 20.04、MySQL 8.0、Redis 6.0、gcc/g++ 10.5.0等,并提供了详细的部署步骤,如安装boost库、编译聊天室服务、配置MySQL和Redis等。最后分析了项目架构,包括数据存储(MySQL存储用户信息,Redis存储房间消息和用户cookie)、消息格式(HTTP请求消息和Websocket交互消息)、HTTP/Websocket数据处理流程等。; 适合人群:有一定Linux基础,想深入了解C++开发及网络编程的开发者,尤其是有志于从事Web开发或服务器端开发的技术人员。; 使用场景及目标:①掌握Linux C++开发环境的搭建,包括工具链的安装与配置;②理解并实践HTTP、Websocket等网络协议的应用;③熟悉MySQL、Redis等数据库的使用;④学习如何处理HTTP请求、Websocket交互消息及数据存储;⑤能够独立完成类似Web聊天室的项目开发。; 其他说明:本文不仅提供了理论指导,还给出了具体的实践操作步骤,如编译过程中可能遇到的问题及解决方案。对于初学者来说,可以按照文中提供的链接和教程逐步学习,同时鼓励读者根据自身需求对项目进行扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值