How to write a message to log file or console using extension method

Vishal Sharma

here's my code

class Program
{
    static void Main(string[] args)
    {
        (from d in new DirectoryInfo("E:\\")
    .GetDirectories() select d.Name)
             .WriteToConsole(Logger.WriteToLog , true);
            //    .WriteToConsole(Console.WriteLine);
    }
}

public static class Logger
{
    public static void WriteToLog(dynamic data)
    {
        string filePath = string.Format("{0}\\log.txt", Environment.CurrentDirectory);
        var stream = File.Create(filePath);
        stream.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetByteCount(data));
        stream.Flush();
        stream.Close();
    }
}

public static class WriteExtensions
{
    public static void WriteToConsole<T1>(this IEnumerable<T1> data , Action<T1> action, bool carelist = false)
    {
        if (carelist)
        {
            // all i want is to invoke action by sending data rather than
    // one by one
            return;
        }
        foreach (var item in data)
        {
            action.Invoke(item);
        }
    }
}

problem is we can write to console line by line..but for writing to file i want to put all at once .. any help.. thanks

Dialecticus

Your logging requirements are revealing to be complex. Therefore I suggest already existing logging frameworks. They have functionalities that you need, but maybe don't yet realize that you need them. Logging to console and log file are supported, and can be enabled in configuration file, instead of in code directly. Also logging to several targets at the same time. Also filtering based on severity of the log. Also rollover to another file when date changes, or file grows over the limit.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to write console.log to a file instead

Python using basicConfig method to log to console and file

Python using basicConfig method to log to console and file

How to disable console.log messages based on criteria from specific javascript source (method, file) or message contents

how to continuously update log file excluding response message of console of django server using logging module?

NodeJS - how to write to a file all the console.log?

How to write a function that writes both to console and log file?

How to always write log message to file regardless of level setting

How do I write a message timestamp to a log file?

How to log to file and to console

File Extension only changed in Console.log

How to write this code using the Linq Extension Method-Syntax?

How to write log to file

How can I show PowerShell error message on the console and write them to the log?

Write Cordova console.log to file

write robocopy output to console and log file

write log to file instead of console in postsharp

How to write a configuration file for chrome extension using a local script

JS: write a join function using reduce method ... console.log of function returns incorrect output

log4perl: How to write ERROR message to file and DEBUG message to stderr?

How to display rainbow message in console.log

Log4J2 - Write only to log file not console

Flask - how to write werkzeug logs to log file using RotatingFileHandler?

how to write to a text file using to log4j?

How to write log to file using slf4j

Printing message on Console without using main() method

How to write for loop for console log of web browser

How do I write Flask's excellent debug log message to a file in production?

How to write a generic extension method in Kotlin?