惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cisco Blogs
Schneier on Security
Schneier on Security
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Scott Helme
Scott Helme
Webroot Blog
Webroot Blog
Project Zero
Project Zero
Google Online Security Blog
Google Online Security Blog
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
Hacker News: Ask HN
Hacker News: Ask HN
PCI Perspectives
PCI Perspectives
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News | PayPal Newsroom
Help Net Security
Help Net Security
The Hacker News
The Hacker News
H
Heimdal Security Blog
O
OpenAI News
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 叶小钗
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
I
Intezer
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security Affairs
P
Proofpoint News Feed
S
Secure Thoughts
腾讯CDC
Google DeepMind News
Google DeepMind News
量子位
罗磊的独立博客

博客园 - Kevin Li

使用WMI远程部署/更新BizTalk程序集 基于WS-AtomicTransaction标准的WCF远程分布式事务(补充) 基于WS-AtomicTransaction标准的WCF远程分布式事务(二) 基于WS-AtomicTransaction标准的WCF远程分布式事务(一) 在Jeffrey Zhao的基础上+反编译System.Web.Extensions.Design得到的完整Ajax代码 .net remoting的事务传播以及wcf分布式事务 在多线程环境下使用HttpWebRequest或者调用Web Service 一篇介绍.NET 2.0 范型挺全面的文章 C# 2.0中的范型和Nullable范型 Windows 2003 下分布式事务协调器(DTC)的配置 使用SQLSourceSafe控制数据库的版本 MySql 主键(自动增加)的数据类型所带来的错误 接口属性集成与智能提示的奇怪问题 动态创建程序集中的类 动态创建程序集中的类 使用NUnit测试包含应用程序配置的dll文件 解决“COM+ 无法与 Microsoft 分布式事务协调程序交谈”的问题 [导入]一些很有用的Reflector 插件 Web安全性原则
使用TraceListener 自动保存跟踪信息到文件中
Kevin Li · 2004-12-08 · via 博客园 - Kevin Li

最近在编写一个使用遗传算法的程序,想跟踪一些中间变量,把它的值写到文件中,但是又不想在算法中加入新的函数来做这种事情,后来想到用TraceListener来保存。通过配置应用程序配置文件app.config,然后在代码中使用Trace.Listeners["myListener"].Write 写跟踪信息,该信息就会被自动记录到文件中去。

配置文件:

<configuration>
<system.diagnostics>
    
<switches>
        
<add name="MagicTraceSwitch" value="3" />
    
</switches>
    
<trace autoflush="false" indentsize="4">
        
<listeners>
           
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\myListener.log" />
           
<!-- You must supply a valid fully qualified assembly name here. --> 
           
<remove type="Assembly text name, Version, Culture, PublicKeyToken"/> 
        
</listeners>
    
</trace>
</system.diagnostics>
 
</configuration>

在listeners 配置节里面的<add .../>中的type是要使用的监听器的类,initializeData是要保存的文件路径。
.NET Framework 1.1自带了几种监听器,但是你可以自己扩展监听器的类型,将调试信息保存到其他地方,如数据库中(类似于net4log)。

在程序代码中的用法:

for( iter = 0; iter < MAX_ITER; iter++ )
            
{
                
if( iter % 300 == 0 )
                
{
                    sppGA.Scale 
*= 0.1;
                }

                
for(int i = 0; i < 10; i++ )
                
{
                    sppGA.CrossOver();
                }

                sppGA.Mutation();
                sppGA.Mutation();
                sppGA.Mutation();
                sppGA.Memory();
                System.Diagnostics.Trace.Listeners[
"myListener"].Write(((Node) sppGA.Individuals[sppGA.Best]).Variables.Transpose().ToString());
                Console.WriteLine(
"Iter:{0} Fitness:{1} Best:{2} Worst{3} Scale:{4} ",iter,((Node) sppGA.Individuals[sppGA.Best]).Fitness,sppGA.Best,sppGA.Worst,sppGA.Scale);
            }