c# - Debug Log mess up my code -


i trying add debug log c#.net code. messes code, looks hell. there log every code values automatically ? looks this

    #if debug          debuglogger("the functionstarted");     #endif     somecode1();      #if debug          debuglogger("somecode1 finished");     #endif     somecode2();      #if debug          debuglogger("somecode2 finished");     #endif     somecode3();      #if debug          debuglogger("somecode3 finished");     #endif     #if debug          debuglogger("function end");     #endif 

pretty late answer leave future reference. in opinion should consider aspect oriented programming tasks this. said if don't need add such complexity small task may move preprocessor conditions log method:

public static void log(string message) { #if debug     // logging #endif } 

do not worry leave empty method, jit optimize away , won't called. it's almost equivalent to:

[condition("debug")] public static void log(string message) 

warning: almost because method [condition] attribute arguments won't evaluated given code in release:

log(string.format("index: {0}", index++)); 

index variable won't ever incremented, because jit compiler won't emit call log , arguments won't evaluated. not true if keep method body empty #if directive inside it. call won't emitted (because of empty body) arguments evaluated.

problem of solution it'll clutter normal program flow. log calls, parameters dumping , stuff that. can do?

refactor log

if call somecode1() method many times shouldn't log @ each call site, better move logging inside called method. log @ beginning , end of each function, log still in code it'll spanned across multiple functions.

void somecode1() {     log("starting somecode1");     //     log("somecode1 completed"); } 

your calling site clean:

somecode1(); somecode2(); somecode3(); 

expressions

if performance aren't issue (measure, don't guess) may use expressions trick you. can log parameters (or fields, object status, diagnostic informations, invariants , whatever else may need), controlled diagnostic switches (to enable them when required). no logging code in lob classes price pay execution speed (and loggedoperation function complexity).

this code (to polite myself) naive, decent implementation more complex think idea more implementation.

static void loggedoperation(expression<action> expression) {     methodcallexpression methodcall = expression.body methodcallexpression;     if (methodcall != null)     log("calling {0}", methodcall.method.name);      expression.compile()();      if (methodcall != null)         log("{0} completed", methodcall.method.name); } 

it'll used this:

loggedoperation(() => somecode1()); loggedoperation(() => somecode2()); loggedoperation(() => somecode3()); 

you'll get:

 calling somecode1 somecode1 completed calling somecode2 somecode2 completed calling somecode3 somecode3 completed 

aop give cleaner code may enough in many situations.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -