将所有控制台输出保存到文本文件

时间:2015-05-24 07:29:53

标签: c#

我正在寻找代理商之类的解决方案,以便将所有控制台输出捕获到数组或文本文件,从程序开头开始。

例如:

<?php
class UploadController extends Controller
{
    function actionIndex()
    {
        $dir = Yii::getPathOfAlias('application.uploads');
        $uploaded = false;

        $model=new Upload();

        if(isset($_POST['Submit'])) {
            //foreach($models as $i=>$model)
            //
            //$files = CUploadedFile::getInstancesByName('file');
            //print_r($model);
            print_r($model);
            //print_r($files);
            /*if($file->validate()){
                $file->file->saveAs($dir.'/'.$file->file->getName());*/
        }else {
            echo 'Error';
        }


            /*$model->attributes=$_POST['Upload'];
            //print_r($files);
            $model->file = CUploadedFile::getInstance($model,'file');
            //$model->file1 = CUploadedFile::getInstance($model,'file');
            //print_r($model->file);

            if($model->validate()){
                    $model->file->saveAs($dir.'/'.$model->file->getName());
            }*/

      $this->render('index', array(
            //'files' => $files,
            'model' => $model,
            //'uploaded' => $uploaded,
            //'dir' => $dir,
       ));
    }
}

我想将操作和函数的所有效果保存到文件中,例如日志。

1 个答案:

答案 0 :(得分:3)

简短回答:Console.SetOut

关于此Link,以下代码将提供帮助

Console.WriteLine("Hello World");

// First, save the standard output.
var oldConsoleWriter = Console.Out;

using(var stream = new FileStream("Test.txt", FileMode.Create))
using(var writer = new StreamWriter(stream))
{  
  Console.SetOut(writer);
  Console.WriteLine("Hello file");
}

Console.SetOut(oldConsoleWriter);
Console.WriteLine("Hello World");