Skip to content

Instantly share code, notes, and snippets.

@mikey-t
Last active October 24, 2024 15:23
Show Gist options
  • Save mikey-t/7999228 to your computer and use it in GitHub Desktop.
Save mikey-t/7999228 to your computer and use it in GitHub Desktop.

Revisions

  1. mikey-t revised this gist Dec 17, 2013. 1 changed file with 54 additions and 54 deletions.
    108 changes: 54 additions & 54 deletions ConsoleOutputLog.cs
    Original file line number Diff line number Diff line change
    @@ -1,78 +1,78 @@
    public class Output
    {
    private readonly string LogDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");
    public class Output
    {
    private readonly string LogDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");

    private static Output _outputSingleton;
    private static Output OutputSingleton
    private static Output _outputSingleton;
    private static Output OutputSingleton
    {
    get
    {
    get
    if (_outputSingleton == null)
    {
    if (_outputSingleton == null)
    {
    _outputSingleton = new Output();
    }
    return _outputSingleton;
    _outputSingleton = new Output();
    }
    return _outputSingleton;
    }
    }

    public StreamWriter SW { get; set; }
    public StreamWriter SW { get; set; }

    public Output()
    {
    EnsureLogDirectoryExists();
    InstantiateStreamWriter();
    }
    public Output()
    {
    EnsureLogDirectoryExists();
    InstantiateStreamWriter();
    }

    ~Output()
    ~Output()
    {
    if (SW != null)
    {
    if (SW != null)
    try
    {
    try
    {
    SW.Dispose();
    }
    catch(ObjectDisposedException){} // object already disposed - ignore exception
    SW.Dispose();
    }
    catch(ObjectDisposedException){} // object already disposed - ignore exception
    }
    }

    public static void WriteLine(string str)
    {
    Console.WriteLine(str);
    OutputSingleton.SW.WriteLine(str);
    }
    public static void WriteLine(string str)
    {
    Console.WriteLine(str);
    OutputSingleton.SW.WriteLine(str);
    }

    public static void Write(string str)
    public static void Write(string str)
    {
    Console.Write(str);
    OutputSingleton.SW.Write(str);
    }

    private void InstantiateStreamWriter()
    {
    string filePath = Path.Combine(LogDirPath, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")) + ".txt";
    try
    {
    SW = new StreamWriter(filePath);
    SW.AutoFlush = true;
    }
    catch (UnauthorizedAccessException ex)
    {
    Console.Write(str);
    OutputSingleton.SW.Write(str);
    throw new ApplicationException(string.Format("Access denied. Could not instantiate StreamWriter using path: {0}.", filePath), ex);
    }
    }

    private void InstantiateStreamWriter()
    private void EnsureLogDirectoryExists()
    {
    if (!Directory.Exists(LogDirPath))
    {
    string filePath = Path.Combine(LogDirPath, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")) + ".txt";
    try
    {
    SW = new StreamWriter(filePath);
    SW.AutoFlush = true;
    }
    catch (UnauthorizedAccessException ex)
    {
    throw new ApplicationException(string.Format("Access denied. Could not instantiate StreamWriter using path: {0}.", filePath), ex);
    Directory.CreateDirectory(LogDirPath);
    }
    }

    private void EnsureLogDirectoryExists()
    {
    if (!Directory.Exists(LogDirPath))
    catch (UnauthorizedAccessException ex)
    {
    try
    {
    Directory.CreateDirectory(LogDirPath);
    }
    catch (UnauthorizedAccessException ex)
    {
    throw new ApplicationException(string.Format("Access denied. Could not create log directory using path: {0}.", LogDirPath), ex);
    }
    throw new ApplicationException(string.Format("Access denied. Could not create log directory using path: {0}.", LogDirPath), ex);
    }
    }
    }
    }
    }
  2. mikey-t renamed this gist Dec 17, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mikey-t created this gist Dec 17, 2013.
    78 changes: 78 additions & 0 deletions ConsoleOutputLog
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    public class Output
    {
    private readonly string LogDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs");

    private static Output _outputSingleton;
    private static Output OutputSingleton
    {
    get
    {
    if (_outputSingleton == null)
    {
    _outputSingleton = new Output();
    }
    return _outputSingleton;
    }
    }

    public StreamWriter SW { get; set; }

    public Output()
    {
    EnsureLogDirectoryExists();
    InstantiateStreamWriter();
    }

    ~Output()
    {
    if (SW != null)
    {
    try
    {
    SW.Dispose();
    }
    catch(ObjectDisposedException){} // object already disposed - ignore exception
    }
    }

    public static void WriteLine(string str)
    {
    Console.WriteLine(str);
    OutputSingleton.SW.WriteLine(str);
    }

    public static void Write(string str)
    {
    Console.Write(str);
    OutputSingleton.SW.Write(str);
    }

    private void InstantiateStreamWriter()
    {
    string filePath = Path.Combine(LogDirPath, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")) + ".txt";
    try
    {
    SW = new StreamWriter(filePath);
    SW.AutoFlush = true;
    }
    catch (UnauthorizedAccessException ex)
    {
    throw new ApplicationException(string.Format("Access denied. Could not instantiate StreamWriter using path: {0}.", filePath), ex);
    }
    }

    private void EnsureLogDirectoryExists()
    {
    if (!Directory.Exists(LogDirPath))
    {
    try
    {
    Directory.CreateDirectory(LogDirPath);
    }
    catch (UnauthorizedAccessException ex)
    {
    throw new ApplicationException(string.Format("Access denied. Could not create log directory using path: {0}.", LogDirPath), ex);
    }
    }
    }
    }