简体   繁体   中英

NULL symbol in file using StreamWriter

I want to add simple logger in to my app. For this purpose I want to use StreamWriter .

Code:

private StreamWriter OutputStream;
OutputStream = new StreamWriter(this.LogFilePath, true);

// .... message - log from app

DateTime now = DateTime.Now;
message = string.Format("[{0:yyyy-MM-dd H:mm:ss}] {1}", now, message
if (OutputStream != null)
{
    OutputStream.WriteLine(message);
    OutputStream.Flush();
}

As result all strings are correctly captured and output is correct, but sometimes it can write empty string with invisible characters at the end:

sample:

 [1970-08-31 14:56:26] Command response ->:c:65:f9:1b:82:97

and if i check this with some tool that can show invisible characters , I can see next:

在此处输入图像描述

As result ~600 lines of log - 125 mb.

I have found that reason could be next:

That happens. When you append a file first its size is corrected in the directory (and that's transactional in NTFS) and then the actual new data is written. There's good chance that if you shut down the system you end up with a file appended with lots of null bytes because data writes are not transactional unlike metadata (file size) writes.

There's no absolute solution to this problem.

Also tried to

check characters with isControl other similar checks;

tried to Trim last characters;

checked docs - looks like all correct

Any advice?

In case someone faced with same issue - reason for me unknown and i may only guess.... but I rewrite logic with log system and bug disappear:

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM