tags:

views:

403

answers:

2

hi, i want log linenumber to my log file, using log4net.the config details of log4net is written programmatically.i tried hell out thing to get line number to be logged but help less,here is my code

private IAppender CreateFileAppender(string name, string fileName) {

        PatternLayout layout = new PatternLayout();            
        //layout.ConversionPattern = "%d{MM/dd/yy HH:mm:ss} %line  %-5p : %m%n";
        //layout.ConversionPattern = "%d{MM/dd/yy HH:mm:ss} 5p [%t] (%F:%L) - %m%n";
        layout.ConversionPattern = "%5p %d{yyyy-MM-dd hh:mm:ss tt} (%c:%L) - [%X           {UserIdentityName}] %m%n";
        layout.ActivateOptions();
        FileAppender appender = new FileAppender(layout, HCVIEWERLOG_FILENAME, true);
        appender.Layout = layout;
        appender.Name = HCVIEWERLOG_APPENDER;
        appender.File = HCVIEWERLOG_FILENAME;
        appender.AppendToFile = true;           
        string[] Args = Environment.CommandLine.Split
            ("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);            
        foreach(string txt in Args)
            appender.Threshold = txt.ToString().ToUpper().Contains("DEBUG") ? 
            log4net.Core.Level.Debug : log4net.Core.Level.Off;
        appender.ActivateOptions();
                    return (appender as IAppender);
    }

even i tried getting line number using stacktrace but im getting the value as 0. here is the code

protected string GetLogMessage(string message) {

        StackTrace stackTrace = new StackTrace(true);
        StackFrame stackFrame = stackTrace.GetFrame(3);
        return (stackFrame.GetMethod().DeclaringType.Name
            + "|" + stackFrame.GetMethod().Name
            + "|" + **stackFrame.GetFileLineNumber()**
            + "||||" + message);
    }
+1  A: 

If you ship your .pdb files along with your .dlls you should get line numbers in your stack trace.

Matt Howells
thanks Matt,when i ship .pdb files along with dll its working, but we ship only dll.is there anyway to get linenumber ?thanks again for ur kindly help
The only way you could do is to specify the line number in the log message - so it would be in your code, and then you'd have to maintain it. Bad idea. Why not just make your log messages unique enough that you can do a quick grep through your code to find the line?
Matt Howells
currently implimented as you mentioned.i thought with line number i will look good, anyway ok now.thanks.
+1  A: 

Two things here:

1) Make sure that the pdb files are present, otherwise you'll always get a zero.

2) Be careful to make sure you are retrieving the correct stack frame. The current stack frame is the zeroth. As you walk up the stack, you will be walking up through your code, BUT above your code on the stack will be a load of framework calls to start the application, and these will have a zero line number.

This bit of code illustrates this:

static void Main(string[] args)
{
    WriteOutStack();
    Console.ReadLine();
}

public static void WriteOutStack()
{
    StackTrace stackTrace = new StackTrace(true);
    for (int i = 0; i < (stackTrace.FrameCount); i++)
    {
        StackFrame stackFrame = stackTrace.GetFrame(i);
        Console.WriteLine("{0}.{1} ({2})", 
            stackFrame.GetMethod().DeclaringType.Name,
            stackFrame.GetMethod().Name,
            stackFrame.GetFileLineNumber());
    }
}

OUTPUT:

Program.WriteOutStack (20)
Program.Main (15)
AppDomain._nExecuteAssembly (0)
HostProc.RunUsersAssembly (0)
ExecutionContext.Run (0)
ThreadHelper.ThreadStart (0)

The output above shows that once I've walked through the two layers that constitute my code, I am into the framework itself for which no line numbers are available.

The main reason I mention this is I noticed, in your sample, you were getting a specific stack frame (frame 3) so I thought it worth raising. (In other words, make sure you don't read the stack upside down in the hand-rolled code you showed above).

Rob Levine
thanks Rob,i tried as u mentioned, yes the basic underline to be is the point 1 mentinoed.pdb file should be shipped along with dll.out of my curiousity , is there anyway we can get line number without using .pdb file ?thanks again.
pretty sure the answer is no. without the pdb, there is nothing to relate your binaries back to the original code from which they came
Rob Levine