Skip to content

Instantly share code, notes, and snippets.

@OlafD
Last active October 23, 2018 10:21
Show Gist options
  • Save OlafD/fa5039ed9c0cfc280872b86f929e8809 to your computer and use it in GitHub Desktop.
Save OlafD/fa5039ed9c0cfc280872b86f929e8809 to your computer and use it in GitHub Desktop.
A simple class to create transcript files for console applications. The filename for the transcript could be constructed by the constructor of the class to have the current date in the filename. Additionally an example for the usage.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TranscriptTester
{
class Program
{
static void Main(string[] args)
{
Tools.Transcript transcript = new Tools.Transcript("C:\\Temp", "TranscriptTester");
transcript.MirrorToConsole = true;
transcript.Write("Program started.");
transcript.WriteWarning("Waring message from the program.");
transcript.WriteError("Error message from the program");
try
{
var x = 0;
var i = 10 / x;
}
catch (Exception ex)
{
transcript.WriteError(ex, true);
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Tools
{
public class Transcript
{
public enum MessageTypes { INF, WRN, ERR }
protected string _filename;
protected bool _mirrorToConsole = false;
public bool MirrorToConsole
{
get { return _mirrorToConsole; }
set { _mirrorToConsole = value; }
}
/// <summary>
/// Initialize the Transcript object with a path and filename
/// </summary>
/// <param name="filename"></param>
public Transcript(string filename)
{
_filename = filename;
}
/// <summary>
/// Initialize the Transcript object with an assembled path and filename
/// </summary>
/// <param name="folder">path, where the file should be stored</param>
/// <param name="title">title-part of the filename</param>
public Transcript(string folder, string title)
{
_filename = String.Format("{0}\\{1}-{2:yyyyMMdd-HHmmss}.txt", folder, title, DateTime.Now);
}
/// <summary>
/// Write a message as an information to the transcript
/// </summary>
/// <param name="message">the message to write</param>
public void Write(string message)
{
Write(message, MessageTypes.INF);
}
/// <summary>
/// Write a message as a warning to the transcript
/// </summary>
/// <param name="message">the message to write</param>
public void WriteWarning(string message)
{
Write(message, MessageTypes.WRN);
}
/// <summary>
/// Write a message as an error to the transcript
/// </summary>
/// <param name="message">the message to write</param>
public void WriteError(string message)
{
Write(message, MessageTypes.ERR);
}
/// <summary>
/// Write the message member of a exception as an error to the transcript
/// </summary>
/// <param name="exception">the exception object to work with</param>
public void WriteError(Exception exception)
{
WriteError(exception, false);
}
/// <summary>
/// Write the message and the stacktrace members of a exception as an error to the transcript
/// </summary>
/// <param name="exception">the exception object to work with</param>
/// <param name="addStacktrace">when true, the stacktrace of the exception is also written to the transcript</param>
public void WriteError(Exception exception, bool addStacktrace)
{
Write(exception.Message, MessageTypes.ERR);
if (addStacktrace == true)
{
Write(exception.StackTrace, MessageTypes.ERR);
}
}
/// <summary>
/// Write a message as an information to the transcript
/// </summary>
/// <param name="message">the message to write</param>
/// <param name="messageType">the type of the message</param>
protected void Write(string message, MessageTypes messageType)
{
if (_mirrorToConsole == true)
{
Console.WriteLine("{0} {1}", messageType, message);
}
if (String.IsNullOrEmpty(_filename) == false)
{
using (StreamWriter sw = new StreamWriter(_filename, true))
{
sw.WriteLine(String.Format("{0:dd.MM.yyyy-HH:mm:ss} {1} {2}", DateTime.Now, messageType, message));
sw.Flush();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment