【问题标题】:How to setup output file in C#, to be enabled only if debug flag = true?如何在 C# 中设置输出文件,仅在调试标志 = true 时启用?
【发布时间】:2015-04-20 17:30:56
【问题描述】:

使用 C#,我想仅在用户指定时(debug=true 标志)打开输出文件(用于调试日志记录)。我尝试了一种以文件打开、写入和关闭为条件的方法。问题是这不会编译,因为调试文件在上下文中不存在。

我想这是因为定义隐藏在条件中,但我不确定如何设置它。如果我不使用条件,任何预先存在的日志都会被清除,这是我不想要的。

请问设置这个的正确方法是什么?

这是我的测试代码:

using System;


public class DataProcessor

{
    public void process_data(string output_filepath, bool debug=false)
    {
        debug = true; // manual override for debugging

        if (debug == true)
        {
            System.IO.StreamWriter debug_file = new System.IO.StreamWriter(output_filepath);
        }

        for (int i=1; i<10; i++)
        {
            // do some other stuff     
            if (debug == true)
            {
                debug_file.WriteLine("output something: " + i);
            }
        }

        // do some other stuff

        if (debug == true)
        {
            debug_file.Close();
        }
    }
}

public class Program
{
    static void Main()
    {
        DataProcessor data = new DataProcessor();

        string output_filepath = "debug_output.txt";
        data.process_data(output_filepath);
    }
}

以下是错误消息:

Microsoft (R) Visual C# 2010 编译器版本 4.0.30319.1 版权所有 (C) 微软公司。保留所有权利。

conditional_stream_open.cs(20,17):错误 CS0103:当前上下文中不存在名称“debug_file”

conditional_stream_open.cs(28,13):错误 CS0103:当前上下文中不存在名称“debug_file”

工具已完成,退出代码为 1

【问题讨论】:

    标签: c# file-io


    【解决方案1】:

    您在 if 块内声明 debug_file。在该上下文之外将无法访问。

    您需要将声明移到方法的顶部:

    System.IO.StreamWriter debug_file = null; // added null here to avoid unassigned variable
    

    然后在 if 语句中:

    debug_file = new System.IO.StreamWriter(output_filepath);
    

    【讨论】:

    • 现在说得通了。但是第二个 if 语句 (WriteLine) 会引发错误:“使用未分配的局部变量 'debug_file'”。我现在正在看这个。
    • 您需要在声明中将 debug_file 初始化为 null:System.IO.StreamWriter debug_file = null;
    【解决方案2】:

    debug_file 超出范围,你应该这样做

    public void process_data(string output_filepath, bool debug=false)
        {
            debug = true; // manual override for debugging
    System.IO.StreamWriter debug_file;
            if (debug == true)
            {
                debug_file = new System.IO.StreamWriter(output_filepath);
            }
    
            for (int i=1; i<10; i++)
            {
                // do some other stuff     
                if (debug == true)
                {
                    debug_file.WriteLine("output something: " + i);
                }
            }
    
            // do some other stuff
    
            if (debug == true)
            {
                debug_file.Close();
            }
        }
    

    使用调试符号#defineDEBUG 代替这种方法进行检查。

    【讨论】:

    • 您需要初始化debug_file = null 否则编译器会将其标记为使用未分配的变量。
    • DEBUG 是编译时符号,而不是运行时符号。它也是在调试版本中自动定义的,所以我认为使用该符号不会产生所需的结果(根据用户选项打开/关闭登录)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2023-02-05
    • 2018-01-28
    • 1970-01-01
    相关资源
    最近更新 更多