Search code examples
loggingmsbuildnlogpostsharpdotfuscator

PostSharp remapping obfuscated code


I am using NLog to create different loggers with different loglevels foreach class. Each logmessage contains the name of the class and the method.

I am also using PostSharp to create code around my methods. The created code basically gets the logger for the current class and writes a logentry.

My problem is that I need to obfuscate my program and want to log the unobfuscated name of the class.

After obfuscating my program it works properly but logs the obfuscated classnames.

I searched the web for solutions and found this blog from PostSharp:

http://www.postsharp.net/blog/post/Whate28099s-New-in-PostSharp-21-Support-for-Obfuscation-%28Dotfuscator%29

The problem is that I don't get how I can implement this "post-obfuscation step (implemented by PostSharp)".

I know how to create a MSBuild project file and how to run it using MSBuild.exe but I don't know what the "PostSharp.AddIn.PostObfuscation.targets" and "PostObfuscationRemap" are doing or how they look like.

I hope that I described my problem properly and that one of you has an answer to it.


Solution

  • The blog post from PostSharp that you found is actually about solving a different problem - the incompatibility of PostSharp 2.1 itself with obfuscation tools. PostSharp 3.0+ is already compatible with obfuscation tools and the blog post is not relevant for these newer versions.

    Your problem is different though - I suspect that your logging code accesses the name of the class during run-time and thus receives an obfuscated class name. You should try to prepare a logging format string during compile time and store it in the aspect's field. This way you can store an original class name in your string and access it during run-time.

    [Serializable]
    public sealed class TraceAttribute : OnMethodBoundaryAspect
    {
        private string enteringMessage;
    
        public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
        {
            string methodName = method.DeclaringType.FullName + method.Name;
            this.enteringMessage = "Entering " + method.DeclaringType.FullName + method.Name;
        }
    
        public override void OnEntry(MethodExecutionArgs args)
        {
            Console.WriteLine(this.enteringMessage);
        }
    }