0

So I'm currently creating a Discord bot with a purge command. However, I want to write to a file the time, author and amount of messages deleted as a log.

I'm using Newtonsoft.Json for this, my code as stands.

using (StreamWriter file = File.CreateText(@"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json")) //create json file at this path.
        {
            JsonSerializer serializer = new JsonSerializer();
            //serialize object directly into file stream
            serializer.Serialize(file, LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages"); //Serialize the time, the author of the command and how many messages they purged
        }

Issue with this code is every time it tries to log given command it just writes over the 1st log. So how do I force it to write to a new line each time? I would preferbly like to keep using this method.

4
  • File.CreateText will create a new file and override any existing one. Do notice that if you do this you won't be able to magically deserialize the file, you'd have to do it manually line by line Commented Feb 4, 2018 at 0:09
  • you are creating each time. you could append to the file name to create a different file each time or use System.IO.File.WriteAllText(filePath, jsonData); to just write to the file that already exists Commented Feb 4, 2018 at 0:12
  • @JoshAdams Thanks, changed it so if there is no file it creates one then writes to it and if there is it just ignores creating one and writes straight to it with a new line! Commented Feb 4, 2018 at 0:41
  • Glad I could help! Do you want me to post a solution or you can post your own :) Commented Feb 4, 2018 at 0:42

2 Answers 2

1

EDIT: My previous answer (below) addressed the question as stated. but as pointed out in a comment, it would not produce a valid json file. just valid json in each line of the file. the following would produce a valid json file with multiple entries:

List<object> log = new List<object>();
            JsonSerializer serializer = new JsonSerializer();
        string path = @"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json";

        if (System.IO.File.Exists(path))
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(path))
            {
                Newtonsoft.Json.JsonReader jreader = new Newtonsoft.Json.JsonTextReader(reader);
                log = serializer.Deserialize<List<object>>(jreader);
            }
        }

        using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(path, false))
        {
            object logEntry = LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages";
            log.Add(logEntry);

            serializer.Serialize(file, log); //Serialize the time, the author of the command and how many messages they purged
        }

Previous Answer: (produces valid json in each line, but not valid json file)

using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(@"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json", true))
        {
            JsonSerializer serializer = new JsonSerializer();
            //serialize object directly into file stream
            serializer.Serialize(file, LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages"); //Serialize the time, the author of the command and how many messages they purged
        }

the "true" value means to append, as opposed to creating a new file or overwriting. Seems CreateText does not come with that option. from the File.CreateText documentation:

This method is equivalent to the StreamWriter(String, Boolean) constructor overload with the append parameter set to false.

File.CreateText

StreamWriter (String, Boolean)

Sign up to request clarification or add additional context in comments.

8 Comments

Note that this will produce invalid JSON (but you obviously know that since it is exactly what you want)...
valid json, each in a seperate line. invalid if the file as its entirety were to be read as a single json object.
"So how do I force it to write to a new line each time?" as asked.
That was not clear from you answer... (and still is not...) Apparently OP had no idea about that since they moved accepted check to other in my opinion less useful answer... Side note: while indeed your answer is nice there are plenty of "how to append to file" answers already - so consider instead flagging as duplicate or at least refer to existing answers if you want to provide particular special case handling.
i agree with you. i edited my answer to show one possible solution of how to achieve a valid json file with multiple entries.
|
0

you are creating each time. you could append to the file name to create a different file each time or use System.IO.File.WriteAllText(filePath, jsonData); to just write to the file that already exists

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.