0

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");
4
  • 4
    You've told us what you've already done, but not asked a queston..... Commented Apr 21, 2017 at 10:22
  • can i know how to get that generated, I don't get what your asking here? Commented Apr 21, 2017 at 10:22
  • stackoverflow.com/questions/10130504/… Commented Apr 21, 2017 at 10:31
  • i have written the above code to get the log generated but it doesn't work and the log file is not getting generated can you please guide me in creating a log file. Commented Apr 21, 2017 at 10:34

1 Answer 1

1

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 -------------

Sign up to request clarification or add additional context in comments.

3 Comments

This worked but i need to rewrite this in my entire application
Why rewrite? I assume that if you didn't logged anything by now, there are only a few occurrences there. Otherwise you can use "Find and Replace" function that is implemented in your favorite IDE?
How is it going? We don't have any news from you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.