Jump to content

The message resource is present but the message is not found in the message table

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Apokal

Apokal

    Newbie

  • Members
  • Pip
  • 1 posts
Hi, everyone!

I hope you will help me...

It's not only C# and .NET, but also WinAPI thread, so sorry if I've missed right forum to post my thread.

Here is my problem:

There is a event provider called "Service Control Manager" under System event log. It's EventMessageFile is %SystemRoot%\system32\services.exe. It contains an event with id = 7036 and this event is "The %1 service entered the %2 state". You can generate it very simple by stopping or running any services in services.msc.

All that I want is to write that event to System event log by myself.

Here is my simple logging code:


using System;

using System.Diagnostics;

using System.Diagnostics.Eventing;

 

class MySample

{

 

 public static void Main()

 {

  

  EventLog myNewLog = new EventLog("System", ".", "Service Control Manager");

  

  myNewLog.WriteEntry("Test",EventLogEntryType.Information, 7036);

 }

}



I run the application with "Run as administrator". Event was written to System log with right event id, source, etc. But the description was "the message resource is present but the message is not found in the string/message table" insted of "The Test service entered the %2 state".



What is my mistake?

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
try it like this:


static void Main(string[] args) {

            string source = "Sample App";

            string log = "Application";


            if (!EventLog.SourceExists(source)) {

                EventLog.CreateEventSource(source, log);

            }

            EventLog.WriteEntry(source, "Hello Event Log!", EventLogEntryType.Information);



            Console.WriteLine("Press <Enter> to terminate...");

            Console.ReadLine();

            return;

        }



For the sake of your sanity, if you're looking for a nice complete logging solution for your application, you should check out log4net or the offering from the enterprise library (Logging Application Block)

Both of these products are configured in your application configuration file, and work like this.
log4net

//somewhere in your class

        private static readonly ILog m_logger = LogManager.GetLogger(typeof(Data<T>));


//then anywhere in that class,w hen you want to write to the log

 m_logger.Error(caught.StackTrace);

or

m_logger.Info("Something Happened");


Logging Application Block:


Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write("Something Happened");


or


Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(caught.StackTrace, "Error"); //Where Error is a custom category created in your logging configuration.



The specifics of hitting the event logger, or a file, or an email address or whatever is handled by the library, and configured. So you can deploy your solution, and change the log listener by modifying a configuration file. You should check it out.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users