Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using NLOG to log the information. There is a property UserStackFrame.

I used the code.

C#
protected override void Write(LogEventInfo loggingEvent)
        {
            try
            {
                var renderedEvent = Layout.Render(loggingEvent);
                var messageBuilder = new GelfMessageBuilder(renderedEvent, HostName, loggingEvent.TimeStamp, ToGelf(loggingEvent.Level))
                    .SetAdditionalField(GelfAdditionalFields.Facility, Facility)
                    .SetAdditionalField(GelfAdditionalFields.LoggerName, loggingEvent.LoggerName);
                if (IncludeSource)
                {
                    var userStackFrame = loggingEvent.UserStackFrame;
                    if (userStackFrame != null)
                    {
                        messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceFileName, userStackFrame.GetFileName());
                        messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceLineNumber, userStackFrame.GetFileLineNumber().ToString(CultureInfo.InvariantCulture));
                    }
                }

But userStackFrame is null. I don't know why and should I need to set up it?

If so, how?
Thanks.
Posted

1 solution

Hello,

In this case and as per LogEventInfo documentation:

UserStackFrame get the stack frame of the method that did the logging.

You need to set the Stack Trace using setStackTrace method of the LogEventInfo class. And just to be sure maybe after do the validation on userStackFrame you can use HasStackTrace method to check that everything is correct just a suggestion.


Code:

C#
protected override void Write(LogEventInfo loggingEvent)
        {
            try
            {
                var renderedEvent = Layout.Render(loggingEvent);
                var stack = new StackTrace();
                int userFrameIndex = stack.FrameCount;

                loggingEvent.setStackTrace(stack,userFrameIndex);

                var messageBuilder = new GelfMessageBuilder(renderedEvent, HostName, loggingEvent.TimeStamp, ToGelf(loggingEvent.Level))
                    .SetAdditionalField(GelfAdditionalFields.Facility, Facility)
                    .SetAdditionalField(GelfAdditionalFields.LoggerName, loggingEvent.LoggerName);
                if (IncludeSource)
                {
                    if(loggingEvent.HasStackTrace)
                    {
                      var userStackFrame = loggingEvent.UserStackFrame;
                    }

                    if (userStackFrame != null)
                    {
                        messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceFileName, userStackFrame.GetFileName());
                        messageBuilder.SetAdditionalField(GelfAdditionalFields.SourceLineNumber, userStackFrame.GetFileLineNumber().ToString(CultureInfo.InvariantCulture));
                    }
                }


I hope this can help you.

Best Regards!
 
Share this answer
 
v2
Comments
[no name] 15-Jul-15 13:00pm    
That's awesome. If you could add code to implement it, it would be great.
Hector Menchaca 15-Jul-15 13:15pm    
Sorry for that!, My post was edited.
[no name] 15-Jul-15 13:37pm    
Why do you use int userFrameIndex = stack.FrameCount;? There are many frames, you select the last one. Have you looked at http://www.codeproject.com/Articles/486286/Trace-Activities-with-NLog?
phil.o 15-Jul-15 15:54pm    
Because, basically, it's a stack? :)
[no name] 15-Jul-15 16:37pm    
I have the additional code.
var exception = loggingEvent.Exception;
if (exception != null)
{
messageBuilder.SetAdditionalField(GelfAdditionalFields.ExceptionMessage, exception.Message);
messageBuilder.SetAdditionalField(GelfAdditionalFields.ExceptionStackTrace, exception.StackTrace);
}

But it(exception.StackTrace) is null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900