Append UTF-8 text to a file in C#
For an occasional text append, File.AppendAllTextAsync manages opening and closing the file. Specify the encoding and create the destination directory first.
Choose a writable application-data location rather than assuming the current directory is writable or stable.
Append one line
Create a .NET console project and replace Program.cs:
using System.Text;
var folder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"TextAppendDemo");
Directory.CreateDirectory(folder);
var file = Path.Combine(folder, "activity.log");
var line = $"{DateTimeOffset.UtcNow:O} Started{Environment.NewLine}";
await File.AppendAllTextAsync(file, line, new UTF8Encoding(false));
Console.WriteLine(file);
Run dotnet run, then inspect the printed file path. Each run adds one timestamped Started line and preserves existing contents.
Decide who owns concurrent writes
This call does not provide a cross-process logging protocol. If several operations write the same file, centralize them through a single writer or use a logging provider designed for concurrency, rotation and retention. A long-lived StreamWriter can reduce repeated open costs but requires an explicit lifetime and flush policy.
Handle I/O and permission failures at the operation boundary. Appending text is also not an atomic replacement strategy for configuration files. Write a separate temporary file and perform the appropriate replacement when that is the required operation.
Official references

Author: Herman Schoenfeld