Customising Method entry Trace message

lakhi_singh's Avatar

lakhi_singh

31 Aug, 2017 05:20 AM

Hi,

I am trying to evaluate PostSharp Diagnostics for one our .Net applications. The results are very promising so far except that I could not find a way to customise the text "Starting" to something followed within the company.

Example: I want to change below
2017-08-31 14:06:24.6012||Trace|DEBUG|SearchApiController.GetSearchSuggestions({Glis.DTO.Input.ClientSearchSuggestionInputDto}, {Glis.Application.Search.GovtLandSearchService})|Starting.
To
2017-08-31 14:06:24.6012||Trace|DEBUG|SearchApiController.GetSearchSuggestions({Glis.DTO.Input.ClientSearchSuggestionInputDto}, {Glis.Application.Search.GovtLandSearchService})|GLIS.InvocationStart.

Reason: These logs are sent to a log server for indexing and can be retreived later.

Thanks!
Lakhwinder

  1. Support Staff 1 Posted by PostSharp Techn... on 01 Sep, 2017 04:45 PM

    PostSharp Technologies's Avatar

    Hello,

    thank you for you interest in PostSharp Diagnostics library.

    This is possible indeed. I assume you want to change 'Starting' to 'GLIS.InvocationStart' and you use a text based logger like NLog or log4net. I use NLog in this example.

    First you have to create a customized version of NLog log record builder and override AppendProlog method:

    public class CustomRecordBuilder : NLogLogRecordBuilder
    {
        public CustomRecordBuilder(NLogLoggingBackend backend) : base(backend) { }
    
        protected override void AppendProlog()
        {
            this.AppendIndentString();
            bool hasContextDescription = this.AppendContextDescription();
            if (this.HasRecordKindName())
            {
                this.AppendDelimiter();
                this.CustomAppendRecordKind();
            }
            else
            {
                if (hasContextDescription)
                {
                    this.RequireDelimiter();
                }
            }
        }
    
        private void CustomAppendRecordKind()
        {
            switch (this.RecordKind)
            {
                case LogRecordKind.MethodEntry:
                case LogRecordKind.CustomActivityEntry:
                    this.AppendParameter("Kind", "GLIS.InvocationStart");
                    break;
                default:
                    base.AppendRecordKind();
                    break;
            }
        }
    }
    

    Then you have to create a customized NLog logging backend:

    public class CustomLoggingBackend : NLogLoggingBackend
    {
        public override LogRecordBuilder CreateRecordBuilder()
        {
            return new CustomRecordBuilder(this);
        }
    }
    

    At this point you can initialize default logging backend with your customized version and you are done:

    LoggingServices.DefaultBackend = new CustomLoggingBackend();
    

    You can change this example for any other text-based logging backend by deriving from appropriate class. For example in case of log4net you would need to change the base classes:

    public class CustomLoggingBackend : Log4NetLoggingBackend
    public class CustomRecordBuilder : Log4NetLogRecordBuilder
    

    -jakub

  2. 2 Posted by lakhi_singh on 04 Sep, 2017 01:44 AM

    lakhi_singh's Avatar

    Thanks I will try it out and post the results here.

  3. 3 Posted by lakhi_singh on 04 Sep, 2017 02:19 AM

    lakhi_singh's Avatar

    Hi Jakub,

    Luckily for me I was using NLog, so could use your code directly. But unfortunately I get "Stackoverflow exception".

    Of course the problems seems to be with "CustomRecordBuilder" class.

    Can you please double check at your end?

    Regards,
    Lakhwinder

  4. Support Staff 4 Posted by PostSharp Techn... on 04 Sep, 2017 10:14 AM

    PostSharp Technologies's Avatar

    Hello,

    The StackOverflow exception can be thrown if the logging is applied to the CustomRecordBuilder class itself. To fix this issue, please apply [Log(AttributeExclude=true)] attribute to the CustomRecordBuilder class as shown on this documentation page: http://doc.postsharp.net/custom-formatter

    -alex

  5. Support Staff 5 Posted by PostSharp Techn... on 13 Sep, 2017 09:26 AM

    PostSharp Technologies's Avatar

    Hello,

    We are going to close this request as we believe it was solved. Please feel free to reopen the discussion if you need more help.

    Best regards,
    PostSharp Team

  6. PostSharp Technologies closed this discussion on 13 Sep, 2017 09:26 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac