Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Services, Threads and EventLogs

Status
Not open for further replies.

bakershawnm

Programmer
Apr 18, 2007
84
0
0
US
I am writing a service that spawns a thread. The thread listens at a socket. I would like the thread to write to the event log. I know how to handle it in VB.NET using the WithEvents.

The problem I am getting is when the thread tries to write to the event log the service (and thread) crashes.

If I have to handle it similar to the way VB.NET does and Raise the event back to the service and let the service write it to the log how do I do that since C# does not have WithEvents?

If I have to do it another way how do I do it?

I am using VS 2008 on Win 7 and .Net 3.5 on Server 2003. The service worked fine until I added the event log.

The last line is where it crashes

using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;
using System.Diagnostics;

namespace SyncSocket
{
public class SynchronousSocketListener
{

// Incoming data from the client.
public static string data = null;

public static void StartListening()
{
EventLog evntlog = new EventLog("Application");

// Data buffer for incoming data.
byte[] bytes = new Byte[1024];

// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);

// Start listening for connections.
while (true)
{
//Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
evntlog.WriteEntry("Waiting for a connection...");

Any help will be much appreciated.

Shawn

 
post the full exception (type, message, stack trace).

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
This would be the exception handler. However no exception is raised.


catch (Exception e)
{
//evntlog.WriteEntry(e.ToString());
}
}


and this is the only thing I get in the event log as a .Net Runtime Error

EventType clr20r3, P1 <my service>.exe, P2 0.0.0.0, P3 4e1f00bc, P4 system, P5 2.0.0.0, P6 4d35319d, P7 38cc, P8 5f, P9 system.argumentexception, P10 NIL.

For more information, see Help and Support Center at
 
My account. I have full admin. Yes it has full rights to write to the log.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top