Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# 使用StreamWriter的文件中的空符号_C#_Android_Ios_Unity3d_Stream - Fatal编程技术网

C# 使用StreamWriter的文件中的空符号

C# 使用StreamWriter的文件中的空符号,c#,android,ios,unity3d,stream,C#,Android,Ios,Unity3d,Stream,我想在我的应用程序中添加简单的记录器。 为此,我想使用StreamWriter 代码: 因此,所有字符串都被正确捕获,输出也正确,但有时它可以写入结尾带有不可见字符的空字符串: 样本: 如果我勾选这个,我可以看到下一个: 结果是约600行日志-125 mb 我有一个理由可以是下一个: 这种情况经常发生。当您首先附加一个文件时,它的大小在 目录(在NTFS中是事务性的),然后是实际的 写入新数据。如果你关掉电脑,很有可能 系统最终会得到一个附加了大量空字节的文件,因为 与元数据(文件大小)写入不

我想在我的应用程序中添加简单的记录器。 为此,我想使用
StreamWriter

代码:

因此,所有字符串都被正确捕获,输出也正确,但有时它可以写入结尾带有不可见字符的空字符串:

样本:

如果我勾选这个,我可以看到下一个:

结果是约600行日志-125 mb

我有一个理由可以是下一个:

这种情况经常发生。当您首先附加一个文件时,它的大小在 目录(在NTFS中是事务性的),然后是实际的 写入新数据。如果你关掉电脑,很有可能 系统最终会得到一个附加了大量空字节的文件,因为 与元数据(文件大小)写入不同,数据写入不是事务性的

这个问题没有绝对的解决办法

也试图

使用
isControl
其他类似检查检查字符

试图
修剪
最后一个字符

选中-看起来一切正常


有什么建议吗?

以防有人遇到同样的问题-我的原因不明,我可能只能猜测。。。。但我用日志系统重写了逻辑,错误消失了:

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

public class EventLogger : MonoBehaviour
{
    private string logFileName = "btlog.txt";
    public bool EchoToConsole = true;
    public bool AddTimeStamp = true;
    public bool EnableFileStorage = true;

    private string LogFilePath
    {
        get
        {
            return Path.Combine(Application.persistentDataPath, logFileName);
        }
    }

    private static EventLogger Singleton = null;
    const string format = "yyyy-MM-dd HH:mm:ss.fffffff";

    public static EventLogger Instance
    {
        get { return Singleton; }
    }

    void Awake()
    {
        if (Singleton != null)
        {
            UnityEngine.Debug.LogError("Multiple EventLogger Singletons exist!");
            return;
        }

        Singleton = this;

        if (this.EnableFileStorage)
        {
            if (File.Exists(LogFilePath))
            {
                long length = new FileInfo(LogFilePath).Length;
                int limit = 1024 * 1024 * 5; // 5mb
                if (length > limit)
                {
                    File.Delete(LogFilePath);
                    Log("log file removed");
                }
            }

            Log("-------------------");
            Log("NEW SESSION STARTED");
        }
    }

    private async Task Write(string message)
    {
        if (this.EnableFileStorage)
        {
            if (AddTimeStamp)
            {
                DateTime now = DateTime.Now;

                string strDate = now.ToString(format);
                string trimmed = new string(message.Where(c => !char.IsControl(c)).ToArray());
                message = string.Format("[{0}] {1}", strDate, trimmed);
            }

            using (StreamWriter outputStream = new StreamWriter(this.LogFilePath, true))
            {
                await outputStream.WriteLineAsync(message);
            }

            if (EchoToConsole)
            {
                UnityEngine.Debug.Log(message);
            }
        }
    }

    [Conditional("DEBUG"), Conditional("PROFILE")]
    public static void Log(string Message)
    {
        if (EventLogger.Instance != null)
        {
            _ = EventLogger.Instance.Write(Message);
        }
        else
        {
            UnityEngine.Debug.Log(Message);
        }
    }
}

你是怎么读文件的?我不写,只是写,加上新行
OutputStream.WriteLine
No,你必须这么做。如果您不阅读该文件,您怎么知道该文件包含空字符呢?我从移动设备下载了该文件,并通过
glogg
在pc上打开本地文件。对于写作,我想我不应该读它,因为
newstreamwriter(this.LogFilePath,true)
-append file and
OutputStream.WriteLine
simple append stream(如果我从@0xBFE1A8正确理解的话)@0xBFE1A8我测试了你的建议,没有任何帮助。。但我更新了一点逻辑,现在看起来它的创建日志文件的大小正确-请检查我的参考答案,并感谢帮助
 [1970-08-31 14:56:26] Command response -> !c:65:f9:1b:82:97
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;

public class EventLogger : MonoBehaviour
{
    private string logFileName = "btlog.txt";
    public bool EchoToConsole = true;
    public bool AddTimeStamp = true;
    public bool EnableFileStorage = true;

    private string LogFilePath
    {
        get
        {
            return Path.Combine(Application.persistentDataPath, logFileName);
        }
    }

    private static EventLogger Singleton = null;
    const string format = "yyyy-MM-dd HH:mm:ss.fffffff";

    public static EventLogger Instance
    {
        get { return Singleton; }
    }

    void Awake()
    {
        if (Singleton != null)
        {
            UnityEngine.Debug.LogError("Multiple EventLogger Singletons exist!");
            return;
        }

        Singleton = this;

        if (this.EnableFileStorage)
        {
            if (File.Exists(LogFilePath))
            {
                long length = new FileInfo(LogFilePath).Length;
                int limit = 1024 * 1024 * 5; // 5mb
                if (length > limit)
                {
                    File.Delete(LogFilePath);
                    Log("log file removed");
                }
            }

            Log("-------------------");
            Log("NEW SESSION STARTED");
        }
    }

    private async Task Write(string message)
    {
        if (this.EnableFileStorage)
        {
            if (AddTimeStamp)
            {
                DateTime now = DateTime.Now;

                string strDate = now.ToString(format);
                string trimmed = new string(message.Where(c => !char.IsControl(c)).ToArray());
                message = string.Format("[{0}] {1}", strDate, trimmed);
            }

            using (StreamWriter outputStream = new StreamWriter(this.LogFilePath, true))
            {
                await outputStream.WriteLineAsync(message);
            }

            if (EchoToConsole)
            {
                UnityEngine.Debug.Log(message);
            }
        }
    }

    [Conditional("DEBUG"), Conditional("PROFILE")]
    public static void Log(string Message)
    {
        if (EventLogger.Instance != null)
        {
            _ = EventLogger.Instance.Write(Message);
        }
        else
        {
            UnityEngine.Debug.Log(Message);
        }
    }
}