It is simple, I want to write something to event log.
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
if (!System.Diagnostics.EventLog.SourceExists("IvrService"))
{
System.Diagnostics.EventLog.CreateEventSource(
"IvrService", "IvrServiceLog");
}
EventLog eventLog1 = new System.Diagnostics.EventLog();
eventLog1.Source = "IvrService";
eventLog1.Log = "IvrServiceLog";
try
{
eventLog1.WriteEntry("Successfully "+State.Stopped.ToString());
IvrApplication.StopImmediate();
}
catch (Exception ex)
{
// eventLog1.WriteEntry(ex.Message);
}
}
The exception is:
Failed to stop service. System.ArgumentException: The source 'IvrService' is not registered in log 'IvrServiceLog'. (It is registered in log 'Application'.) " The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property.
at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName)
at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message)
The error message is telling you exactly what is wrong. You have the Event Source IvrService registered with the Application Log, not the IvrServiceLog. The System.Diagnostics.EventLog.SourceExists verifies that the source exists, but not for a particular log.
My guess is that you originally registered this with the Application log and then later changed it to write to the IvrServiceLog.
To clean up your development machine, you could simply run the following and then you code should work going forward.
System.Diagnostics.EventLog.DeleteEventSource("IvrService");
Related
I've been using the following basic code which I took from a website to write messages to the Windows Event Log using the EventLog class from System.Diagnostics
class Program
{
static void Main(string[] args)
{
WriteEventLogEntry("This is an entry in the event log");
}
private static void WriteEventLogEntry(string message)
{
// Create an instance of EventLog
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
// Check if the event source exists. If not create it.
if (!System.Diagnostics.EventLog.SourceExists("TestApplication"))
{
System.Diagnostics.EventLog.CreateEventSource("TestApplication", "Application");
}
// Set the source name for writing log entries.
eventLog.Source = "TestApplicationNotExisting";
// Create an event ID to add to the event log
int eventID = 8;
// Write an entry to the event log.
eventLog.WriteEntry(message,
System.Diagnostics.EventLogEntryType.Error,
eventID);
// Close the Event Log
eventLog.Close();
}
}
I've tweaked the code above so that the Source that is being selected is one that I have not previously created using the CreateEventSource method. I expected this to throw an exception but instead it works perfectly well and I have a message in the Event Log from source "TestApplicationNotExisting".
Why is this not throwing an exception? if I can select sources on the fly then what's the point of the CreateEventSource method?
I would like to know if how can we catch the event in C# when a system event log is created. Also I would like to know if how can I get only ERROR LOGS from Windows event logs using C#.
I have the following code but it just returns me all logs. I just need ERROR logs:
System.Diagnostics.EventLog eventLog1 = new System.Diagnostics.EventLog("Application", Environment.MachineName);
int i = 0;
foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
{
Label1.Text += "Log is : " + entry.Message + Environment.NewLine;
}
You can use CreateEventSource static method of EventLog class to create event log like
EventLog.CreateEventSource("MyApp","Application");
EventLog class present in System.Diagnostics namespace.
You can use WriteEntry() method to write to the event log. EventLogEntryType enum can be used to specify the type of event you want to log. An example below
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 234);
See How to write to an event log by using Visual C#
if you are looking for reading only ERROR level log then you can use the below code block. You just need to check EntryType of the eventlog entry and then print/display accordingly.
static void Main(string[] args)
{
EventLog el = new EventLog("Application", "MY-PC");
foreach (EventLogEntry entry in el.Entries)
{
if (entry.EntryType == EventLogEntryType.Error)
{
Console.WriteLine(entry.Message);
}
}
}
I have window service which acts as a sync software. I want to add unhanded exception logging on my service, so I modified my program.cs like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
static void Main()
{
// Register Unhandled Exception Handler
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
// Run Service
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service()
};
ServiceBase.Run(ServicesToRun);
}
static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
// Get Exception
Exception ex = (Exception)args.ExceptionObject;
// Generate Error
string ErrorMessage = String.Format(
"Error: {0}\r\n" +
"Runtime Terminating: {1}\r\n----- ----- ----- ----- ----- -----\r\n\r\n" +
"{2}\r\n\r\n####################################\r\n",
ex.Message,
args.IsTerminating,
ex.StackTrace.Trim());
// Write Error To File
try
{
using (StreamWriter sw = File.AppendText("UnhandledExceptions.log"))
sw.WriteLine(errorMessage);
}
catch { }
}
}
Then on my Service.cs file, in the OnStart method, I added a throw new Exception("test"); to see if unhanded exceptions are being logged to file as expected.
When I start my service, it stops immediately as expected; however it doesn't seem to be logging the exception to the specified file.
Any idea what I am doing wrong here? Thanks in advance for any help.
Before you ask, my service runs as Local Service and the directory where my service .exe runs from (c:\mysync) already has Local Service added in the security tab with full read/write access.
OnStart is called in Service base class inside try-catch block. If an exception happens on this stage it catches it and just set a status 1 as a result and do not throw it further:
string[] args = (string[]) state;
try
{
this.OnStart(args);
.....
}
catch (Exception ex)
{
this.WriteEventLogEntry(Res.GetString("StartFailed", new object[1]
{
(object) ((object) ex).ToString()
}), EventLogEntryType.Error);
this.status.currentState = 1;
}
As a result you can find a record in EventLogs, but you can't catch it as an unhanded domain exception, as there is no such exception.
using (StreamWriter sw = File.AppendText("UnhandledExceptions.log"))
It is forever a really bad idea to not use full path names for files (like c:\foo\bar.log). Especially in a service, you have very little control over the default directory for your service. Because it is started by the service control manager, not by the user from the command prompt or a desktop shortcut.
So high odds that you are just looking at the wrong file. The real one probably ended up being written to c:\windows\system32 (or syswow64). The operating system directories are normally write protected but that doesn't work for a service, they run with a highly privileged account so can litter the hard drive anywhere.
Always use full path names. Using the EventLog instead is highly recommended.
We're using XMS.Net to connect to a WebSphere MQ server V7; this has always worked fine with a V6 server but since the "other party" has upgraded to V7 we expierienced some trouble. Most of it has been fixed but now I've stumbled upon an error that I can't explain, nor find anything about:
CWSMQ0282E: A null value has been used for argument BUFFER = <> NULL within method ImportMQMDMesageBuffer(WmqSession, WmqDestination, MQMD,byte[],int,int).
The preceding method detected an invalid null argument.
If necessary, recode the application to avoid the error condition.
Stacktrace: at IBM.XMS.Client.WMQ.WmqReceiveMarshal.ImportMQMDMesageBuffer(MQMessageDescriptor mqmd, Byte[] buffer, Int32 dataStart, Int32 dataEnd)
at IBM.XMS.Client.WMQ.WmqAsyncConsumerShadow.Consumer(Phconn hconn, MQMessageDescriptor mqmd, MQGetMessageOptions mqgmo, Byte[] pBuffer, MQCBC mqcbc)
at IBM.WMQ.Nmqi.UnmanagedNmqiMQ.NmqiConsumerMethodUM(Int32 hconn, IntPtr structMqmd, IntPtr structMqgmo, IntPtr buffer, IntPtr structMqcbc)
The only thing I think I know about the cause of this error is that we sent a message and I'm expecting the CoA and CoD messages; I'm expecting these to be in the queue and when I shut down my consumer listening for these messages the rest works fine.
I have absolutely no idea what is going on...
EDIT
This is the minimum testcase:
using System;
using System.Configuration;
using System.Text;
using IBM.XMS;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
//Setup unhandled exception "logging"
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//Change this to your own needs!
string QueueManager = "CONTOSO";
string Channel = "MYCOMPANY.CONTOSO.TCP";
string Queue = "MYCOMPANY.REPORTQ";
string HostIP = "192.168.1.29"
int Port = 1416;
//Create connection
var factoryfactory = XMSFactoryFactory.GetInstance(XMSC.CT_WMQ);
var connectionfactory = factoryfactory.CreateConnectionFactory();
connectionfactory.SetStringProperty(XMSC.WMQ_QUEUE_MANAGER, QueueManager);
connectionfactory.SetStringProperty(XMSC.WMQ_HOST_NAME, HostIP);
connectionfactory.SetIntProperty(XMSC.WMQ_PORT, Port);
connectionfactory.SetStringProperty(XMSC.WMQ_CHANNEL, Channel);
connectionfactory.SetIntProperty(XMSC.WMQ_BROKER_VERSION, XMSC.WMQ_BROKER_V2);
connectionfactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT_UNMANAGED);
var connection = connectionfactory.CreateConnection();
connection.ExceptionListener = new ExceptionListener(OnXMSExceptionReceived);
//Create session
var session = connection.CreateSession(false, AcknowledgeMode.ClientAcknowledge);
//Create consumer
var queue = session.CreateQueue(string.Format("queue://{0}/{1}", QueueManager, Queue));
queue.SetIntProperty(XMSC.WMQ_TARGET_CLIENT, XMSC.WMQ_TARGET_DEST_MQ); //Prevent automatic RFH (or JMS) headers in messages...
var consumer = session.CreateConsumer(queue);
consumer.MessageListener = new MessageListener(OnMessageReceived); //Messages received will be handled by OnMessageReceived
//Start the connection (which starts the consumer to listen etc.)
Console.WriteLine("Starting");
connection.Start();
Console.WriteLine("Started; press any key to stop");
//Now we wait...
Console.ReadKey();
//Tear down the connection
Console.WriteLine("Stopping");
connection.Stop();
Console.WriteLine("Stopped; press any key to end application");
//Keep the console around
Console.ReadKey();
}
private static void OnMessageReceived(IMessage message)
{
Console.WriteLine("Message received");
if (message is IBytesMessage)
{
var bytesmsg = (IBytesMessage)message;
var data = new byte[bytesmsg.BodyLength];
Console.WriteLine(Encoding.UTF8.GetString(data));
}
else
{
//The message is not an IBytesMessage, check to see if it is a Feedback-type message
if (message.PropertyExists(XMSC.JMS_IBM_FEEDBACK))
{
//Figure out which type of feedback message this is
int feedback = message.GetIntProperty(XMSC.JMS_IBM_FEEDBACK);
switch (feedback)
{
case MQC.MQFB_COA:
Console.WriteLine("COA received");
break;
case MQC.MQFB_COD:
Console.WriteLine("COD received");
break;
default:
//Unknown feedback type
Console.WriteLine("Unknown feedback");
break;
}
}
else
{
//The message is not a feedback message; we don't know what this is so it's unexpected.
Console.WriteLine("Unexpected message received");
}
}
Console.WriteLine("Acknowledging");
message.Acknowledge();
Console.WriteLine("Acknowledged");
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//Uh oh
Console.WriteLine("*** UnhandledException ***");
Console.WriteLine((e.ExceptionObject as Exception).Message);
Console.WriteLine("******************************");
}
private static void OnXMSExceptionReceived(Exception ex)
{
//Uh oh
Console.WriteLine("*** OnXMSExceptionReceived ***");
Console.WriteLine(ex.Message);
Console.WriteLine("******************************");
}
}
}
Create a new (console)project, add a reference to IBM.XMS.dll (C:\Program Files (x86)\IBM\WebSphere MQ\Tools\Lib\IBM.XMS.dll) and run the project. Put any message in the report queue and see what happens.
When connected to a V6 server everything is fine, V7 results in the exception being thrown.
Also we tried updating to 2.1.0.1 but to no avail...
EDIT
Here's what I'm seeing:
This is my trace log (Sorry, can't add it here since my message will be >30000 chars long) and here is an even more detailed log (traceSpecification "all" instead of "debug").
I also tried switching the (test)application to .Net V2.0.50727.5456 but that doesn't help either.
EDIT
I seem to have narrowed it down to "empty" CoA's and CoD's; when messages are sent with the MQRO_COA_WITH_DATA or MQRO_COA_WITH_FULL_DATA (same for CoD) as opposed to MQRO_COA then the CWSMQ0282E error doesn't occur. So XMS.Net seems to crash on empty bodies for CoA's and CoD's. I need to confirm some things to ensure it's not caused by other stuff in my project interfering but I'm quite sure this is the cause.
The exception appears to be because the received message does not have message body. If the message received is because of either MQRO_COD or MQRC_COA option (set when the original message was sent) would not have any message body. When XMS tries to process messages without any body, it's getting into trouble.
I am puzzled as to how this was working when using MQ v6. You may want to check if the application that's sending the original message has been changed off late.
Also for XMS to process any message, the incoming message must contain required JMS headers. MQRO_COD/MQRO_COA are automatically generated by the queue manager and will not contain JMS headers.
Few other suggestions on the code snippet above:
1) An instance of IPEndpoint is not really required. You can simply set hostname or IP address as a string and port number as integer.
2) XMSC.RTT_BROKER_PING_INTERVAL need not be set when connecting WMQ.
3) Since you have usedAcknowledgeMode.AutoAcknowledgewhile creating a session, no need to call message.Acknowledge() in OnMessageReceivedmethod.
As far as I can determine this exception, indeed, occurs on "empty" CoA's en CoD's. When messages are sent with the MQRO_COA_WITH_DATA / MQRO_COD_WITH_DATA (or even bulkier MQRO_COA_WITH_FULL_DATA / MQRO_COD_WITH_FULL_DATA) the exception doesn't occur. We will file a "PMR" with IBM to confirm.
Have a windows service that listens to a msmq. In the OnStart method is have this
protected override void OnStart(string[] args)
{
try
{
_queue = new MessageQueue(_qPath);//this part works as i had logging before and afer this call
//Add MSMQ Event
_queue.ReceiveCompleted += new ReceiveCompletedEventHandler(queue_ReceiveCompleted);//this part works as i had logging before and afer this call
_queue.BeginReceive();//This is where it is failing - get a null reference exception
}
catch(Exception ex)
{
EventLogger.LogEvent(EventSource, EventLogType, "OnStart" + _lineFeed +
ex.InnerException.ToString() + _lineFeed + ex.Message.ToString());
}
}
where
private MessageQueue _queue = null;
This works on my machine but when deployed to a windows 2003 server and running as Network service account, it fails
Exception recvd:
Service cannot be started. System.NullReferenceException: Object reference not set to an instance of an object.
at MYService.Service.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
Solved:
Turned out that the Q that i set up, I had to explicitly add Network Service account to it under security tab
You're seeing that particular exception because you're calling ex.InnerException.ToString(). The InnerException property is not always populated (in fact, it frequently isn't, nor should it be).
Your root problem is likely that the Network Service account doesn't have permissions to access the queue (in this case, read from it).
Here's some code that will help you get the actual error in your event log:
catch(Exception ex)
{
Exception e = ex;
StringBuilder message = new StringBuilder();
while(e != null)
{
if(message.Length > 0) message.AppendLine("\nInnerException:");
message.AppendLine(e.ToString());
e = e.InnerException;
}
EventLogger.LogEvent(EventSource, EventLogType, "OnStart" + _lineFeed +
message.ToString());
}