Contents tagged with OpenSource

  • Integrating .NET and Erlang using OTP.NET


    Summary:
    A simple example using OTP.NET to connect a .NET node to an erlang node. I have decided to write this after reading the article on "Integrating Java and Erlang" on ServerSide.com so I highly recommend that you read that article before continuing here as there are many things which are already described there which I'm not going to repeat.

     

    OTP.NET is a port to the Jinterface that the author used in the serverside article.  Here, I'll use the same process to integrate erlang code with a C# application.

     

    [I found OTP.NET while browsing through the Jungerl code repository. Jungerl contains miscellanous utilities for erlang programmers]

     

    The source code for the erlang mathserver application is also listed here. I've changed it to do multiplcation instead of addition and made it simpler by removing the send/receive code.

     

    File: Mathserver.erl


    -module(mathserver).

    -compile(export_all).

     

    multiply(First, Second) ->

    First * Second.

     

    Now, you can try this code in an erlang shell to see if it is working

     

    (1) >> werl.exe -sname servernode -setcookie cookie

    (2) (servernode@apollo)3> c(mathserver).

    {ok,mathserver}

    (3) (servernode@apollo)7> mathserver:multiply(10, 3).

    30

     

    (1) starts an erlang shell in a window using servernode as the short node name. The cookie is also required for security and you can see how we will use it in the client.

    (2) & (3) we then compile our application and call the exposed method to get the result back.

     

    Before starting to write a C# Sharp application, we can quickly start another erlang shell to see if we can connect to our server node from a new node.

    >> werl.exe -sname clientnode -setcookie cookie

    >> (clientnode@apollo)3> rpc:call(servernode@apollo, mathserver, multiply, [10, 2]).

    20

     

    Once we are confident that the erlang application is working, we can close the client node shell and start devenv.exe and a new project .

     

    The first thing we need to do is to include the reference to the OTP.dll file. You can either compile it yourself by downloading the source code from jungerl[sourceforge] or just download the sample application(sample download link). Please note that I've only included the dll in the sample and not the full source code.

     

    Now you can add the following code to the main method.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Otp;
    
    namespace ErsharpClient 
    {
        class Program
        {
            static void Main(string[] a)
            {
                OtpSelf cNode = new OtpSelf("clientnode", "cookie");
                OtpPeer sNode = new OtpPeer("servernode@apollo");
                OtpConnection connection = cNode.connect(sNode);
    
                Otp.Erlang.Object[] args = new Otp.Erlang.Object[] { 
                    new Otp.Erlang.Long(1), new Otp.Erlang.Long(4)
                }; 
                connection.sendRPC("mathserver", "multiply", args);
    
                Otp.Erlang.Long sum = (Otp.Erlang.Long)connection.receiveRPC();
                Console.WriteLine("Return Value:" + sum.ToString());
            }
        }
    }

    This is exactly similar to the code described in the article therefore I'm not going to duplicate that information.

     

    Download Sample Application 

     

    Other Links:

    [ServerSide Article] http://www.theserverside.com/tt/articles/article.tss?l=IntegratingJavaandErlang

    [JInterface] http://www.erlang.org/doc/apps/jinterface/index.html

    [OTP.NET Announcement] http://www.erlang.org/pipermail/erlang-questions/2004-May/012313.html

    [Jungerl] http://jungerl.sourceforge.net/

    [OTP.NET] http://jungerl.cvs.sourceforge.net/jungerl/jungerl/lib/otp.net/

     

    So, Java comes to rescue .NET again and OTP.NET makes inter node communication between erlang and .NET possible :)

    Read more...

  • Logging made easy (or Log4net for beginners)

    Download Sample  (EasyLogging.zip)

    Log4net is the best tool for implementing logging inside your application. It is fast, provides all the necessary appenders and an open source community behind its development. But, even with all these functionalities, it is not recommended to embed log4net code inside your application code. Rather, you must abstract it behind a customized Logger class so that you can replace it with any other tool without replacing the references in all the places within your code. I am sure that most of the teams are already following this practice but in case you are starting with Log4net or a novice programmer then don’t forget this practice. For more information about log4net, read this article. http://www.ondotnet.com/pub/a/dotnet/2003/06/16/log4net.html

     

    In this short article, I will demonstrate how to integrate log4net into your application by following 3 simple steps. The information in this article may not be sufficient for every possible scenario but I found these applicable for most of the projects.

     

    1)      Add reference to the log4net.dll. This is very simple and I am sure that everybody knows how to add a reference in VS.NET.

     

    2)      Copy the following class code and paste it inside your application in a new class. This singleton class abstracts the log4net library and provides simple methods for your application.

     

    Logger.cs

        1 using System;

        2 using log4net;

        3 using System.Diagnostics;

        4 using System.IO;

        5 

        6 [assembly:log4net.Config.DOMConfigurator()]

        7 [assembly: log4net.Config.Domain()]

        8 

        9 namespace WinGhost

       10 {

       11     /// <summary>

       12     /// Summary description for Logger.

       13     /// </summary>

       14     public class Logger

       15     {

       16         log4net.ILog log = log4net.LogManager.GetLogger("Logging");

       17 

       18         private static volatile Logger _current = null;

       19         private static object syncRoot = new object();

       20 

       21         private Logger()

       22         {}

       23 

       24         public static Logger Current

       25         {

       26             get

       27             {

       28                 if (_current == null)

       29                 {

       30                     lock(syncRoot)

       31                     {

       32                         if (_current == null)

       33                         {

       34                             _current = new Logger();

       35                         }

       36                     }

       37                 }

       38 

       39                 return _current;

       40             }

       41         }

       42 

       43         public void Debug(string message)

       44         {       

       45             if (this.log.IsDebugEnabled)

       46                 this.log.Debug("[" + DateTime.Now.ToString() + "] " + ExtractInfo(message));

       47         }

       48 

       49         public void Info(string message)

       50         {           

       51             if (this.log.IsInfoEnabled)

       52                 this.log.Info("[" + DateTime.Now.ToString() + "] " + ExtractInfo(message));

       53         }

       54 

       55         public void Error(string message, System.Exception e)

       56         {

       57             if (this.log.IsErrorEnabled)

       58                 this.log.Error("[" + DateTime.Now.ToString() + "] " + ExtractInfo(message), e);

       59         }

       60 

       61         public void Error(string message)

       62         {

       63             if (this.log.IsErrorEnabled)

       64                 this.log.Error("[" + DateTime.Now.ToString() + "] " + ExtractInfo(message));

       65         }

       66 

       67         private string ExtractInfo(string message)

       68         {

       69             StackFrame frame1 = new StackFrame(2, true);

       70             string methodName = frame1.GetMethod().ToString();

       71             string fileName = Path.GetFileName(frame1.GetFileName());

       72             string[] textArray1 = new string[6] { "File:", fileName, " - Method:", methodName, " - ", message } ;

       73 

       74             return string.Concat(textArray1);

       75         }

       76     }

       77 }

    3)      Copy the following configuration settings and copy it into web.config or app.config depending on your application settings.

     

    *.Config

     

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

    <configuration>

    xml version="1.0" encoding="utf-8" ?>

    <configuration>

    configuration>

    <configSections>

    <configSections>

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,

    log4net-net-1.0" />

    log4net-net-1.0" />

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,

    log4net-net-1.0" />

    />

    </configSections>

    </configSections>

    <log4net>

    <log4net>

    <logger name="Logging"></logger>

    <logger name="Logging"></logger>

    <root>

    <root>

    <level value="DEBUG" />

    <level value="DEBUG" />

    <appender-ref ref="FileAppender" />

    <appender-ref ref="FileAppender" />

    <appender-ref ref="UdpAppender" />

    <appender-ref ref="UdpAppender" />

    </root>

    </root>

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

    <file value="TestLog.txt" />

    <file value="TestLog.txt" />

    <appendToFile value="false" />

    <appendToFile value="false" />

    <datePattern value="yyyyMMdd" />

    <datePattern value="yyyyMMdd" />

    <rollingStyle value="Date" />

    <rollingStyle value="Date" />

    <staticLogFileName value="true" />

    <staticLogFileName value="true" />

    <layout type="log4net.Layout.PatternLayout">

    <layout type="log4net.Layout.PatternLayout">

    <conversionPattern value="%n[%t] %m%n" />

    <conversionPattern value="%n[%t] %m%n" />

    </layout>

    </layout>

    </appender>

    </appender>

    <appender name="FileAppender" type="log4net.Appender.FileAppender">

    <appender name="FileAppender" type="log4net.Appender.FileAppender">

    <file value="TestLog.txt" />

    <file value="TestLog.txt" />

    <appendToFile value="false" />

    <appendToFile value="false" />

    <layout type="log4net.Layout.PatternLayout">

    <layout type="log4net.Layout.PatternLayout">

    <conversionPattern value="%n[%t] %m%n" />

    <conversionPattern value="%n[%t] %m%n" />

    </layout>

    </layout>

    </appender>

    </appender>

    <appender name="UdpAppender" type="log4net.Appender.UdpAppender">

    <appender name="UdpAppender" type="log4net.Appender.UdpAppender">

    <param name="RemoteAddress" value="127.0.0.1" />

    <param name="RemoteAddress" value="127.0.0.1" />

    <param name="RemotePort" value="8080" />

    <param name="RemotePort" value="8080" />

    <layout type="log4net.Layout.XmlLayout">

    <layout type="log4net.Layout.XmlLayout">

    <param name="Prefix" value="" />

    <param name="Prefix" value="" />

    </layout>

    </layout>

    </appender>

    </appender>

    </log4net>

    </configuration>

    </configuration>

    </log4net>

    </configuration>

    configuration>

     

     

    NOTE: The above files are also provided in the sample attached with this article.

     

    After following these steps, you are ready to use logging in your application and a simple statement like

     

    Logger.Current.Debug(…) or Logger.Current.Error(…) prints the message to the appropriate output depending on the settings specified in the configuration file. If you are following the settings mentioned in step 3, then you‘ll find a file named “TestLog.txt” in your bin directory. Before you start to log every little condition to log file, please classify the different conditions where you need to use Debug or Error method. I personally use Error(…) for logging exceptions or error messages and Debug(…) for everything else. This way, you can turn off Debug(…) messages by replace

    <level value="DEBUG" /> with

    <level value="ERROR" />

     

    Other Tools:

    Here are some other tools are I use frequently with log4net.

     

    BareTail: BareTail is a real time viewer for log files (or any text file).

    http://www.baremetalsoft.com/baretail/index.php

     

    Log4Net Viewer: A very nice log viewer for lo4net, especially useful in situations where you don’t have control over your deployment environment. There is a UdpAppender section in my configuration file so you can test it with the sample project without making any changes.

    Tip: Use the log filter text box to filter messages from any file, method.

    http://www.devintelligence.com/Log4NetViewer/

     

    Resources:


    Log4net FAQ:
    http://log4net.sourceforge.net/release/1.2.0.30316/doc/manual/faq.html

    Using log4net:
    http://www.ondotnet.com/pub/a/dotnet/2003/06/16/log4net.html

    Configuration:

    http://logging.apache.org/log4net/release/manual/configuration.html

    Examples:

    http://logging.apache.org/log4net/release/example-apps.html

    Other Interesting Links:
    How To Capture Web Events In log4net

    [NEW] A realistic log4net config

     

     

    Read more...