chaitanya Phani
chaitanya Phani

Reputation: 1

Creating Log file for C# windows Application

I have written a code block to generate log file so that I can get all the functions being called.

I used the Microsoft.Practises.EnterpriseLibrary.Logging dll to get the log file generated can I know how to get that generated.

string procName = "reports_plan_and_proj.plan_v_paid_inv_pay_method_sum";
Logger.Write("SQL:GetPlanvsPaidInvestPmtMthdSummary():: " + procName, "SQL");

Upvotes: 0

Views: 14226

Answers (1)

Sven Borden
Sven Borden

Reputation: 1090

Let's use a class somehow like this one

public class LogControl
{
    private static string _Path = string.Empty;
    private static bool DEBUG = true;

    public static void Write(string msg)
    {
        _Path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        try
        {
            using (StreamWriter w = File.AppendText(Path.Combine(_Path, "log.txt")))
            {
                Log(msg, w);
            }
            if (DEBUG)
                Console.WriteLine(msg);
        }
        catch(Exception e)
        {
            //Handle
        }
    }

    static private void Log(string msg, TextWriter w)
    {
        try
        {
            w.Write(Environment.NewLine);
            w.Write("[{0} {1}]", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
            w.Write("\t");
            w.WriteLine(" {0}", msg);
            w.WriteLine("-----------------------");
        }
        catch(Exception e)
        {
                        //Handle
        }
    }
}

You call it using LogControl.Write("I want to log this") And it produce an output like this : [1:19:34 PM Friday, April 21, 2017] I want to log this -------------

Upvotes: 1

Related Questions