Sending the LINQ To SQL log to log4net

DataContext Log Property can be used to print generated SQL query or command in the console window as shown below:

db.Log = Console.Out;

If you want to print the SQL query in DebugView or VS Output window you may use DebugWriter class(by Kris Vandermotten) .The usage of the class is pretty simple …

db.Log = new DebugWriter();

I wrote a small class that help you redirect DataContext messages to log4net .I’ve changed the DebugWriter and call it Log4NetWriter . The usage is still simple

db.Log = new Log4NetWriter(this.GetType());

As result you get formatted log4net messages that can be send to any log4net appender .

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net;
using log4net.Core;
using log4net.Repository.Hierarchy;
 
namespace Devintelligence.Data
{
 
 
    public class Log4NetWriter : TextWriter
    {
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Log4NetWriter"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        public Log4NetWriter(Type type):this(type,Level.Debug)
        {
        }
 
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Log4NetWriter"/> class.
        /// </summary>
        /// <param name="type">The logger source type.</param>
        /// <param name="level">The log4net log level.</param>
        public Log4NetWriter(Type type, Level level)
        {
            logSrcType = type;
            this.level = level;
            open = true;
        }
 
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="T:System.IO.TextWriter"/> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            open = false;
            base.Dispose(disposing);
        }
 
        /// <summary>
        /// Writes a character to the text stream.
        /// </summary>
        /// <param name="value">The character to write to the text stream.</param>
        /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        public override void Write(char value)
        {
            if (!open)
            {
                throw new ObjectDisposedException(null);
            }
            Log(value.ToString());
        }
 
        /// <summary>
        /// Writes a string to the text stream.
        /// </summary>
        /// <param name="value">The string to write.</param>
        /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        public override void Write(string value)
        {
            if (!open)
            {
                throw new ObjectDisposedException(null);
            }
            if (value != null)
            {
                Log(value);
            }
        }
 
        /// <summary>
        /// Writes a subarray of characters to the text stream.
        /// </summary>
        /// <param name="buffer">The character array to write data from.</param>
        /// <param name="index">Starting index in the buffer.</param>
        /// <param name="count">The number of characters to write.</param>
        /// <exception cref="T:System.ArgumentException">The buffer length minus <paramref name="index"/> is less than <paramref name="count"/>. </exception>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="buffer"/> parameter is null. </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        ///     <paramref name="index"/> or <paramref name="count"/> is negative. </exception>
        /// <exception cref="T:System.ObjectDisposedException">The <see cref="T:System.IO.TextWriter"/> is closed. </exception>
        /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
        public override void Write(char[] buffer, int index, int count)
        {
            if (!open)
            {
                throw new ObjectDisposedException(null);
            }
            if (buffer == null || index < 0 || count < 0 || buffer.Length - index < count)
            {
                base.Write(buffer, index, count); // delegate throw exception to base class
            }
            Log( new string(buffer, index, count));
        }
 
        /// <summary>
        /// When overridden in a derived class, returns the <see cref="T:System.Text.Encoding"/> in which the output is written.
        /// </summary>
        /// <value></value>
        /// <returns>The Encoding in which the output is written.</returns>
        public override Encoding Encoding
        {
            get
            {
                if (encoding == null)
                {
                    encoding = new UnicodeEncoding(false, false);
                }
                return encoding;
            }
        }
 
 
        /// <summary>
        /// Writes a message to log4net 
        /// </summary>
        /// <param name="message"></param>
        private void Log( string message )
        {
            Hierarchy hierarchy = ((Hierarchy)LogManager.GetRepository());
            ILoggerFactory loggerFactory = ((Hierarchy)LogManager.GetRepository()).LoggerFactory;
            Logger logger = hierarchy.GetLogger(logSrcType.FullName, loggerFactory);
            logger.Log(level, message, null);
        }
 
 
        private bool open;
        private static UnicodeEncoding encoding;
        private readonly Level level;
        private Type logSrcType;
    }
}

 

Technorati Tags: ,,,

LINQPad(beta) – a great little application for LINQ expression testing

The LINQPad(beta) is an advanced and free LINQ expressions testing and learning tool written by Joseph Albahari. It allows you to develop and verify a LINQ expression.LINQPad supports LINQ to objects, LINQ to SQL and LINQ to XML. To run LINQ against SQL queries, you will need SQL Server 2005, SQL Server 2000, or SQL Express.
LINQPad requires .NET Framework 3.5 Beta 2 (NOT the May 2006 CTP, nor Framework 3.5 Beta 1)
LINQPad is fully compatible with Windows Vista and does not require administrative elevation.

 

linqpad

Download LINQPad

Technorati Tags: LINQ, dotnet, orcas, vs2008

LINQ(Language-Integrated Query) CTP (May 2006) is available for download

This release adds significantly to the LINQ functionality previously available. Highlights of this release include:

  • Productivity enhancements via DLINQ designer and debugger support within the Visual Studio 2005 IDE.
  • Support for a broader range of development scenarios thanks to new databinding and ASP.NET support.
  • Ability to integrate LINQ with existing code through features like LINQ over DataSet and DLINQ improvements including inheritance
  • Feedback driven features including deep stored procedure support, since the earlier CTP.

The installer contains:

  • LINQ libraries
  • VB and C# language enhancements
  • Visual Studio IDE additions
  • Samples (VB or C#)
  • LINQ Overview
  • Standard Query Operators white paper
  • Getting Started papers
  • DLinq overview
  • DLinq Hands-on-labs
  • XLinq Overview
  • XLinq Hands-on-labs
  • References and language specifications

Get Linq May 2006 CTP

[Via Fabrice's weblog]

Technorati Tags:  link dlink csharp library release update