Thursday, June 2, 2011

dot.NET C# – Write to Windows Event Log

dot.NET  C# – Write to Windows Event Log
For some special case, you might need to write the log to Windows Event Log,
To kick start, create a Class so that you can alwiz reuse the code when want to write to Event Log. In my case, I’ve created a Class called EventLogHelper:

class EventLogHelper
    {
        public static void writeEventLog(String description)
        {
            String sSource;
            String sLog;

            sSource = "MyService";
            sLog = "Application";

            if (!EventLog.SourceExists(sSource))
            {
                EventLog.CreateEventSource(sSource, sLog);
            }

            EventLog.WriteEntry(sSource, description);
        }

    }
So, from your codes, you just need to call the Helper class whenever you want to log to Event Log:
EventLogHelper.writeEventLog("Testing 123");
For the first timer, you could encounter the following exception:
“The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.” System.Exception {System.
Security.SecurityException}

If you ecounter the exception above, then you will need to manually create New Event Source Entry in registry.
Do do that , access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\
For instance, the below screen shots & steps are taken from Windows 7:
(1) Go to run, type regedit
regedit
(2) Windows will prompt ” Do you allow the following program to make changes on this computer?”
Click “yes”
(3) To to this path :
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application
(4) Add New Key
Right Click Application –> New –> Key
(5) Name the new key as “MyService”
sample code, dot.NET  C# – Write to Windows Event Log 



(6) Run your application
(7) Check the Event Log
Windows –> Control Panel –> Administrator –> Event Viewer

sample code, dot.NET , C# – Write to Windows Event Log

Saturday, May 28, 2011

How to Remove HttpSoap12 in webservice SOAP 1.2 in .NET Framework 2.0

How to Remove HttpSoap12 - SOAP 1.2 in .NET Framework 2.0


The new ASMX runtime in .NET 2.0 supports SOAP 1.2. At this moment SOAP 1.1 is most widely being used in the industry. In the .NET Framework both SOAP 1.1 and SOAP 1.2 are supported. This means that the Web Services created in .NET Framework 2.0 will be configured to support both SOAP 1.1 and SOAP 1.2 messages. This indirectly means that the WSDLs thus created for the Web Service will have two types of bindings, i.e., SOAP 1.1 and SOAP 1.2.


<wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
<wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
 
 
Whether both these bindings have to be added to the web service can be configured by enabling or disabling them from the web.config file.

<configuration>
  <system.web>
    <webServices>
      <protocols>
        <remove name="HttpSoap12"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>