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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

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