Func<string> interface for logging

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Func<string> interface for logging

Rikard Pavelic
How come there isn't

public void Log(LogLevel level, Func<string> func)

interface in NLog2 for .NET 3.5?

It would be cool if there would be interface like that, because that looks to me like the fastest way to call logging (no need to evaluate arguments).

Regards,
Rikard
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Func<string> interface for logging

Jarek Kowalski
Administrator

Actually, there is:

        /// <summary>
        /// Writes the diagnostic message at the specified level.
        /// </summary>
        /// <param name="level">The log level.</param>
        /// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
        public void Log(LogLevel level, LogMessageGenerator messageFunc)
        {

        }

I could not use Func<string> because it is not available in .NET 2.0 and .NET CF 2.0, so NLog defines its own delegate:

    /// <summary>
    /// Returns a log message. Used to defer calculation of
    /// the log message until it's actually needed.
    /// </summary>
    /// <returns>Log message.</returns>
    public delegate string LogMessageGenerator();

It does not really matter when you're using lambda syntax, anyway since the compiler will try to match the method signature to whatever delegate type is expected, so you can do:

logger.Log(LogLevelInfo, () => "hello world");

In C# 2.0 you can use delegate syntax:

logger.Log(LogLevelInfo, delegate() { return "hello world" });

Jarek

 

 

NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Func<string> interface for logging

Rikard Pavelic
I see. Thanks.
But now there's another problem ;)
I'm using Trace, Info and Error methods, so compiler matches my call to
public void Trace<T>(T value)
instead of
public void Trace(LogMessageGenerator messageFunc)

I can work around this, but it's an gotcha.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Func<string> interface for logging

alon
In reply to this post by Jarek Kowalski
Hi,

How is it possible to pass a parameter to the LogMessageGenerator function?

I would like to achieve this:

        Dim sw As New Stopwatch()
        sw.Start()
...
        sw.Stop()
        If sw.ElapsedMilliseconds > 5000 Then
            Logger.Trace("ShootLocations took {0} ms", sw.ElapsedMilliseconds)
        End If

like something like this (I know the lambda is incorrect...):
        Dim sw As New Stopwatch()
        sw.Start()
...
        sw.Stop()
Logger.Log(LogLevel.Trace, New LogMessageGenerator(Function(sw) If sw.ElapsedMilliseconds > 5000 Then Return String.Format(ShootLocations took {0} ms", sw.ElapsedMilliseconds)))

Thanks.
Loading...