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

推荐订阅源

D
Docker
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
罗磊的独立博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
T
Tailwind CSS Blog
IT之家
IT之家
博客园 - 聂微东
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Security Affairs
宝玉的分享
宝玉的分享
V
V2EX
C
Cisco Blogs
博客园 - Franky
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
S
Securelist
J
Java Code Geeks
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
WordPress大学
WordPress大学
W
WeLiveSecurity
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志

博客园 - 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);
            }