Posts Tagged ‘log4net’

Logging in Mail.dll

Monday, August 1st, 2016

To enable logging for Mail.dll clients (Imap, Pop3, Smtp) you only need to add the following line before you connect:

// C# version:

Limilabs.Mail.Log.Enabled = true;

' VB.NET version:

Limilabs.Mail.Log.Enabled = True

You can observe the log output by:

  • looking at the Visual Studio’s output window (View/Output/’Show output from’: Debug)
  • subscribing to Log.WriteLine event
  • defining custom listeners using your application’s config file (App.config or Web.config)
  • using log4net

This is how the log looks like in the Visual Studio’s output window:

You can also enable logging using your application’s config file (App.config, Web.config):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>

      <switches>
        <add name="Mail.dll" value="Verbose" />
      </switches>

    </system.diagnostics>
</configuration>

log4net

If you are using the latest version of log4net.dll, Mail.dll is going to use log4net instead of standard .NET System.Net.TraceSource class. Please refer to log4net manual on how to capture log entries.

Mail.dll uses logger called “Mail.dll” (_logger = LogManager.GetLogger(“Mail.dll”)) and level Info (_logger.Info(message)) to log information.

Please remember that even when using log4net, you need to enable logging by setting “Limilabs.Mail.Log.Enabled = True” or by setting Mail.dll trace switch in the config file (App.config, Web.config) to Verbose as shown above.

Log to file

You’ll need to define a TextWriterTraceListener that writes to a file and connect it with Mail.dll trace source. The easiest solution is to modify your application’s config file (App.config, Web.config) accordingly:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>

        <trace autoflush="true"/>
   
        <switches>
            <add name="Mail.dll" value="Verbose"/>
        </switches>
   
        <sources>
            <source name="Mail.dll">
                <listeners>
                    <add name="MailLogFile"/>
                </listeners>
            </source>
        </sources>
   
        <sharedListeners>
            <add 
                name="MailLogFile" 
                type="System.Diagnostics.TextWriterTraceListener" 
                initializeData="c:\folder-with-write-access\mail.log"/>
        </sharedListeners>

    </system.diagnostics>
</configuration>

Log.WriteLine

Log class exposes WriteLine event. You can use that event to subscribe your own logging library.

// C#

Limilabs.Mail.Log.WriteLine += Console.WriteLine;
' VB.NET

Limilabs.Mail.Log.WriteLine += Console.WriteLine

I have problems issuing IMAP, POP3 or SMTP command

Wednesday, January 5th, 2011

Mail.dll is a rock solid product, however most email servers don’t follow RFC specifications rigorously. In the following few steps we’ll help you gather the information we need to make Mail.dll clients better.

If you have problems parsing a message please go here.

Prerequisites

First please check if you have the latest version installed. You can always download the latest version of Mail.dll email component and release notes here.

Identify command

Please identify the command/set of commands that cause problems.

Turn on logging

Enable logging for Mail.dll clients:

// C# version:

Log.Enabled = true;

' VB.NET version:

Log.Enabled = True

You can observe standard Visual Studio’s trace output window (View/Output/Show output from: Debug) or add a custom listener to Trace.Listeners collection.

You can also enable logging using App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
      <switches>
        <add name="Mail.dll" value="True" />
      </switches>
    </system.diagnostics>
</configuration>

log4net

If you are using the latest version of log4net.dll, Mail.dll is going to use log4net instead of Trace class. Please refer to log4net manual on how to capture log entries.

Please remember that even when using log4net, you need to enable logging by setting “Limilabs.Mail.Log.Enabled = True” or be setting Mail.dll boolean switch in App.config file.

Log to file

You can create your custom TraceListener that writes log to file:

// C# version:

internal class MyListener : TextWriterTraceListener
{
    public MyListener(string fileName)
        : base(fileName)
    {
    }

    public override void Write(string message, string category)
    {
        if (category == "Mail.dll")
            base.Write(message, category);
    }

    public override void WriteLine(string message, string category)
    {
        if (category == "Mail.dll")
            base.WriteLine(message, category);
    }
}
' VB.NET version

Friend Class MyListener
	Inherits TextWriterTraceListener
	Public Sub New(fileName As String)
		MyBase.New(fileName)
	End Sub

	Public Overrides Sub Write(message As String, category As String)
		If category = "Mail.dll" Then
			MyBase.Write(message, category)
		End If
	End Sub

	Public Overrides Sub WriteLine(message As String, category As String)
		If category = "Mail.dll" Then
			MyBase.WriteLine(message, category)
		End If
	End Sub
End Class

…then add it to Listeners collection:

// C# version:

Log.Enabled = true;
Trace.Listeners.Add(new MyListener(@"c:/mail_log.txt"));
Trace.AutoFlush = true;

' VB.NET version:

Log.Enabled = True
Trace.Listeners.Add(New MyListener("c:/mail_log.txt"))
Trace.AutoFlush = True

Log.WriteLine

Log class exposes WriteLine event. You can use that event to subscribe your own logging library.

// C#

Log.WriteLine += Console.WriteLine;
' VB.NET

Log.WriteLine += Console.WriteLine

Reproduce the problem

Perform the operations that cause the problems, and save the log to file.
You can remove username and password, but please do not modify the file extensively. If you do, it may be impossible to reproduce the issue.
In particular do not change new line format nor encoding.

Additional information

Please answer following questions:

  • What exception are you getting?
  • What is the exception stack trace?
  • What is the exception message?
  • What result you expect?
  • What result are you getting?
  • Which .NET Framework version are you using?
  • Is it console, windows forms, windows service or web application?
  • If it is possible please attach the source code you are using

Sending

Finally please zip the log file and send it as attachment, along with all the answers to .

Thank you!

Logging in Ftp.dll

Friday, November 5th, 2010

To enable logging for Ftp.dll .NET FTP and FTPS component you only need to add the following line before you connect:

// C# version:

Limilabs.FTP.Log.Enabled = true;

' VB.NET version:

Limilabs.FTP.Log.Enabled = True

You can observe the log output by:

  • looking at the Visual Studio’s output window (View/Output/’Show output from’: Debug)
  • subscribing to Log.WriteLine event
  • defining custom listeners using your application’s config file (App.config or Web.config)
  • using log4net

This is how the log looks like in the Visual Studio’s output window:

You can also enable logging using your application’s config file (App.config, Web.config):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>

      <switches>
        <add name="Ftp.dll" value="Verbose" />
      </switches>

    </system.diagnostics>
</configuration>

log4net

If you are using the latest version of log4net.dll, Ftp.dll is going to use log4net instead of standard .NET System.Net.TraceSource class. Please refer to log4net manual on how to capture log entries.

Ftp.dll uses logger called “Ftp.dll” (_logger = LogManager.GetLogger(“Ftp.dll”)) and level Info (_logger.Info(message)) to log information.

Please remember that even when using log4net, you need to enable logging by setting “Limilabs.FTP.Log.Enabled = True” or by setting Ftp.dll trace switch in the config file (App.config, Web.config) to Verbose as shown above.

Log to file

You’ll need to define a TextWriterTraceListener that writes to a file and connect it with Ftp.dll trace source. The easiest solution is to modify your application’s config file (App.config, Web.config) accordingly:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>

        <trace autoflush="true"/>
   
        <switches>
            <add name="Ftp.dll" value="Verbose"/>
        </switches>
   
        <sources>
            <source name="Ftp.dll">
                <listeners>
                    <add name="FtpLogFile"/>
                </listeners>
            </source>
        </sources>
   
        <sharedListeners>
            <add 
                name="FtpLogFile" 
                type="System.Diagnostics.TextWriterTraceListener" 
                initializeData="c:\folder-with-write-access\ftp.log"/>
        </sharedListeners>

    </system.diagnostics>
</configuration>

Log.WriteLine

Log class exposes WriteLine event. You can use that event to subscribe your own logging library.

// C#

Limilabs.FTP.Log.WriteLine += Console.WriteLine;
' VB.NET

Limilabs.FTP.Log.WriteLine += Console.WriteLine

You can download Ftp.dll FTP/FTPS client for .NET here.

Programmatically check Log4Net log

Monday, November 30th, 2009

Today I needed to create unit test that checked if the NHibernate query was generated optimally.

Good thing is that, in case of an inefficient query, NHibernate puts a warning using log4net:

log.Warn( "firstResult/maxResults specified with collection fetch; applying in memory!" );

I wanted something like this:

[Test]
public void FindUsersBy_QueryWithLimit_LimitsOnSQLSide()
{
    using (LogChecker logChecker
        = new LogChecker("NHibernate", Level.Warn))
    {
        // Execute query using NHibernate

        // ....

        Assert.IsEmpty(logChecker.Messages);
    }
}

The problem is that it’s not that easy to attach MemoryAppender for a duration of unit test to the specified logger.

Anyway here’s the code:

public class LogChecker : IDisposable
{
    readonly Logger _logger;
    readonly Level _previousLevel;
    readonly MemoryAppender _appender = new MemoryAppender();

    public LogChecker(string logName, Level levelToCheck)
    {
        _logger = (Logger)LogManager.GetLogger(logName).Logger;
        _logger.AddAppender(_appender);
        _previousLevel = _logger.Level;
        _logger.Level = levelToCheck;
    }

    public List<string> Messages
    {
        get
        {
            return new List<loggingEvent>(_appender.GetEvents())
                  .ConvertAll(x => x.RenderedMessage);
        }
    }

    public void Dispose()
    {
        _logger.Level = _previousLevel;
        _logger.RemoveAppender(_appender);
    }
};