Giter Club home page Giter Club logo

debuglogger's Introduction

.NET

DebugLogger

This library exports a DebugLogger, an implementation of ILogger for Debugging purposes, that are:

  • Simple string print
  • Printing variables in a pretty way
  • Printing the variables passed to a function as arguments whenever the function is called

Initialization

You can instantiate a DebugLogger like this:

var logger = new DebugLogger(
    new DebugLogger.DebugLoggerConfig() 
        { 
            OutWriter = writer, 
            MethodsWithContextLogging = new HashSet<MethodBase>() 
                {
                    typeof(YourClass).GetMethod("YourMethod"),
                    typeof(Convert).GetMethod("ToInt32", BindingFlags.Static | BindingFlags.Public, new Type[] 
                        {
                            typeof(bool)
                        }
                    ),
                    typeof(Uri).GetMethod("ToString")
                }
        }
    );

Where:

  • OutWriter is of type TextWriter and its default value is Console.Out.
  • MethodsWithContextLogging is an ISet<MethodBase> containing all the methods that, when called, should print the arguments used to call them. You can also change methods defined in external libraries or even in the System namespace.

Among the DebugLoggerConfig you can also specify the IsEnabled property, corresponding to the ILogger.IsEnabled() and a VariableFormatter: it accepts a variable name and value and returns a string that defaults to {name} = {value}. It is used in the LogVariables method.


Logging

Each method contained in MethodsWithContextLogging is decorated with a prefix to extend the method itself and log the initial values of the arguments passed to it. A prefix is a concept coming from Harmony, a library that "patches" methods at runtime.

Basically, every time these methods called, their arguments are logged on a TextWriter, that is:

  • The value of OutWriter if the class declaring the method has a field (the accessibility is not important) of type DebugLogger.
  • Console.Out otherwise, including the case when the method called is a static one.

Limitations

  • The maximum number of arguments that a method in MethodsWithContextLogging can have is 7, but can be easily extended.
  • The prefix in Harmony is a class containing only static methods, therefore reading the logger configurations involves some constraints. In the current implementation, configs are available only if there is a field of type DebugLogger in the class declaring the method intercepted.
  • You might encounter an exception when trying to patch certain methods belonging to the System namespace because (here I guess) they are internally used by Harmony.

Conclusions

A DebugLogger can be very useful when you have a stack trace and you need more information for a certain frame, or just when you would like to follow how an algorithm works without having to scatter log calls in each method.

debuglogger's People

Contributors

marco-luzzara avatar

Watchers

James Cloos avatar  avatar

Forkers

folkin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.