Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串太小,无法存储进程的标准输出_C#_String_Logging - Fatal编程技术网

C# 字符串太小,无法存储进程的标准输出

C# 字符串太小,无法存储进程的标准输出,c#,string,logging,C#,String,Logging,我引导进程的标准输出/错误,并将其存储在一个字符串中,并将其记录到一个文件中 我注意到并不是所有的标准输出都存储在文本文件中,所以我必须达到字符串的大小限制 有人知道有什么替代方案可以解决这个问题吗 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Loggin

我引导进程的标准输出/错误,并将其存储在一个字符串中,并将其记录到一个文件中

我注意到并不是所有的标准输出都存储在文本文件中,所以我必须达到字符串的大小限制

有人知道有什么替代方案可以解决这个问题吗

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Logging
{
    class Logger
    {
        private FileStream file = null;
        private StreamWriter writer = null;

        public Logger(string _path)
        {
            if (!File.Exists(_path)) File.Create(_path).Dispose();

            file = new FileStream(_path, FileMode.Append, FileAccess.Write, FileShare.Write);
            writer = new StreamWriter(file);
        }

        public void Log(string _text)
        {
            Console.WriteLine(_text);
            using (file)
            {
                writer.WriteLine("[" + DateTime.Now + "] : " +  _text);
            }
        }
    }
}
你刚刚处理了你的文件。你不能再给它写信了


你刚刚处理了你的文件。您无法再向其写入。

字符串类型的限制为2GB

您对文件创建和处理的处理给您带来了一些困难

请尝试使用以下更简洁的日志记录代码:

        const string log = @"logfile path";
        public void Log(string _text)
        {
            try
            {
                using (TextWriter tw = new StreamWriter(_path, true))
                {
                    tw.WriteLine("[" + DateTime.Now + "] : " +  _text);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

字符串
类型的限制为2GB

您对文件创建和处理的处理给您带来了一些困难

请尝试使用以下更简洁的日志记录代码:

        const string log = @"logfile path";
        public void Log(string _text)
        {
            try
            {
                using (TextWriter tw = new StreamWriter(_path, true))
                {
                    tw.WriteLine("[" + DateTime.Now + "] : " +  _text);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

字符串没有合理的大小限制。(你的进程没有写入2GB的输出)在我看来,这可能是因为你没有刷新写入程序。我的心理调试技能告诉我,你在吞咽所有异常而不读取它们。不要这样做。也许使用类似NLog的东西,而不是明显不起作用的东西。刷新修复了问题,在这里找到了一个有用的链接:字符串没有合理的大小限制。(你的进程没有写入2GB的输出)在我看来,这可能是因为你没有刷新写入程序。我的心理调试技能告诉我,你在吞咽所有异常而不读取它们。不要这样做。可能使用类似NLog的东西,而不是明显不起作用的东西。刷新解决了问题,在这里找到了一个有用的链接: