1
\$\begingroup\$
private void log(MethodExecutionArgs args, string message, LogLevel level)
{            
    var calling_ip = ((System.Web.Services.WebService)args.Instance).Context.Request.UserHostAddress;
    string method = args.Method.Name;
    LogEventInfo logInfo = new LogEventInfo(level, logger.Name, message);
    logInfo.Properties.Add("method", method);
    logInfo.Properties.Add("calling_ip", calling_ip);
    logger.Log(logInfo);
}

I have written this method to log the ip address of the caller. The calling_ip value is accessed in the NLog.config via the event_context ${event-context:item=calling_ip}

Can this code be improved?

\$\endgroup\$
2
  • \$\begingroup\$ What kind of improvements are you looking for? \$\endgroup\$
    – svick
    Apr 23 '14 at 11:17
  • \$\begingroup\$ svick, this works. But I was curious to know if there were other ways of doing this. \$\endgroup\$
    – Siddharth
    Apr 23 '14 at 11:38
1
\$\begingroup\$

One possible change would be to use object initializer to set up the properties:

var logInfo = new LogEventInfo(level, logger.Name, message)
{
    Properties = { { "method", method }, { "calling_ip", calling_ip } }
};

But I'm not sure this actually improves the code much, especially since using this syntax this way is uncommon, so it can be confusing.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.