Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
5.7k views
in .NET FTP by (51.6k points)
Redirecting EDT's own logging messages to log4net is easy.

Firstly, turn off EDT's own log writers as follows:
EnterpriseDT.Util.Debug.Logger.CurrentLevel = EnterpriseDT.Util.Debug.Level.OFF;

then handle the LogMessageReceived event in the EnterpriseDT.Util.Debug.Logger class. It's static, so the handler will also need to be static:
EnterpriseDT.Util.Debug.Logger.LogMessageReceived += new EnterpriseDT.Util.Debug.LogMessageHandler(Logger_LogMessageReceived);
.
.
.
static void Logger_LogMessageReceived(object sender, EnterpriseDT.Util.Debug.LogMessageEventArgs e)
{
    log4net.Core.Level l4nLevel = GetL4NLevel(e.LogLevel);
    if (log.Logger.IsEnabledFor(l4nLevel))
        log.Logger.Log(typeof(FTPConnection), l4nLevel, e.FormattedText, e.Exception);
}

and finally add the GetL4NLevel method which convert EDT's logging levels to log4net's logging levels:
private static log4net.Core.Level GetL4NLevel(EnterpriseDT.Util.Debug.Level edtLevel)
{
    if (edtLevel == EnterpriseDT.Util.Debug.Level.FATAL)
        return log4net.Core.Level.Fatal;
    else if (edtLevel == EnterpriseDT.Util.Debug.Level.ERROR)
        return log4net.Core.Level.Error;
    else if (edtLevel == EnterpriseDT.Util.Debug.Level.WARN)
        return log4net.Core.Level.Warn;
    else if (edtLevel == EnterpriseDT.Util.Debug.Level.INFO)
        return log4net.Core.Level.Info;
    else if (edtLevel == EnterpriseDT.Util.Debug.Level.DEBUG)
        return log4net.Core.Level.Debug;
    else if (edtLevel == EnterpriseDT.Util.Debug.Level.ALL)
        return log4net.Core.Level.Verbose;
    else
        return log4net.Core.Level.Off;
}


- Hans (EnterpriseDT)

2 Answers

0 votes
by (140 points)
Does anyone have an example of diverting/redirecting EDT's logging to another logger, that is in VB.Net?

The code I've tried doesn't seem to get any events.

Thank you!
0 votes
by (51.6k points)
Here's some VB code that redirects logging to the console:
Imports EnterpriseDT.Net.Ftp
Imports EnterpriseDT.Util.Debug

Module Module1

    Sub Main()
        AddHandler Logger.LogMessageReceived, AddressOf Logger_LogMessageReceived
        Logger.CurrentLevel = Level.OFF

        Dim ftp As New SecureFTPConnection
        Try
            ftp.ServerAddress = "ftp.gnu.org"
            ftp.UserName = "ftp"
            ftp.Password = "test"
            ftp.Connect()
            For Each f As String In ftp.GetFiles
                Console.Out.WriteLine(f)
            Next
            ftp.Close()
        Catch ex As Exception
            Console.Error.WriteLine(ex.Message)
        End Try
        Console.In.ReadLine()
    End Sub

    Private Sub Logger_LogMessageReceived(sender As Object, e As LogMessageEventArgs)
        Console.Out.WriteLine(e.LogLevel.ToString() & ": " & e.FormattedText)
    End Sub

End Module


- Hans (EnterpriseDT)

Categories

...