IFileLogger myLogger = new TextFileLogger("log. We provide very simple and powerful tool for creating, editing and sharing text documents. Public class TextFileLogger : IFileLoggerĪnd here there is an example of how we would use it: Protected virtual void Dispose(bool disposing)Īll put together, our logger class looks like this: To implement the Log method, we will use the WriteLine method of the stream, just like this:Īnd last, but not least we need to implement the Dispose method: System.IO.File has various methods to create an StreamWriter among them AppendText creates an stream that appends text to an existing file, or to a new file if the specified file does not exist yet. It also only contains standard characters, which are those found in the default set of characters that an application can display. It does not take any special formatting, such as varying fonts, font sizes, bold font, or italics. For starters we need to create a StreamWriter. Plain Text To put it simply, plain text is any text that isn’t formatted. Public interface IFileLogger:ILogger,IDisposable We know that we need to follow the ILogger interface, and also at this point we know we are going to work with text files, meaning we need to allow the user to liberate that resource when he has finished writing. Now whenever you launch TextEdit, it will open a plain text document. In there you can set the New Document preference to Plain Text. We have been charged with the implementation of the TextFileLogger: the class that logs to a plain text file. 10.4: Create new blank text files via contextual menuAuthored by: DavidRavenMoon on Mar 28, 07 02:05:53PM. The document will automatically be saved using Untitled. Here we got the interface our logger has to implement: When you are finished creating the plain text file, tap the Done button. To make a bit less boring let´s imagine we are implementing a logger library that would allow us to log to different sources. To illustrate the points above we are going to create a TextFileLogger class whose only purpose is to append data a text file. Create additional text files by clicking + New Text File button.
NEW PLAIN TEXT DOCUMENT DOWNLOAD
Edit, Share, Save to Drive as a Doc or Download the file to your device. Notice that all Stream classes implement IDisposable to allow us to neatly liberate resources when those are not longer needed. Then enter text in the text box or open a text file using the Open File button. It is important to remember to close the StreamWriter as soon as we are done to avoid performance issues, and memory leaks.
To use a StreamWriter we only need to do the following: When we are working with files, and need to write text data – as opposed to writing bytes – we will use an StreamWriter. A plain text document is a kind of document that doesnt preserve formatting for example links or words in bold, italics, with underline, images, colors. One of the interesting points of Streams is that they can improve the performance of this data transfers by breaking data into smaller chunks.Ĭ# has different kinds of stream with its particular functionality. Streams are an abstraction layer that allows us to transfer data from one source to another. In C# we usually use streams to read and write data to files.