PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] How to put all console output in a file but also display it on console C#?

 August 18, 2022     c#, console, output     No comments   

Issue

How I do this in C#? I know about Console.SetOut() and it's not what i want since i get nothing on my console and everything goes to file. How I do to make both write in text file and display on Console? Is there any smart way or I need to go the hard and tedious route of Check file exists, Open File, Write, Close File after each Console.WriteLine() function


Solution

You could use a simple logger class:

class Logger {
    string LogFilePath { get; set; }

    void WriteLine(string msg) {
        Console.WriteLine(msg);
        System.IO.File.AppendAllText(LogFilePath, msg + '\n');
    }
}

Usage:

var logger = new Logger { LogFilePath = "text file path" };
logger.WriteLine("Write this to the console and append to the logfile");


Answered By - ayylmao
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing