c# - Tcp windows service not creating any log file -


i have created windows service open port , log data in text file.it asynchronous data communication allow connect multiple clients.but problem is not creating log file not logging data.kindly guide me doing wrong??? here code...

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.diagnostics; using system.linq; using system.net; using system.net.sockets; using system.serviceprocess; using system.text; using system.threading.tasks; using system.configuration; using system.io;  namespace tcpservice {     public partial class service1 : servicebase     {         public service1()         {             initializecomponent();         }          protected override void onstart(string[] args)         {             setupserver();         }          public void ondebug()         {             onstart(null);         }          protected override void onstop()         {             service1._serversocket = null;         }          private static byte[] _buffer = new byte[1024];         private static list<socket> _clientsockets = new list<socket>();         private static int port = 10000;         private static ipaddress iparr = ipaddress.parse("192.168.1.12");         private static socket _serversocket = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp);         private static void setupserver()         {             //console.writeline("setting server");             _serversocket.bind(new ipendpoint(iparr, port));             _serversocket.listen(100);             _serversocket.beginaccept(new asynccallback(acceptcallback), null);         }          private static void acceptcallback(iasyncresult ar)         {             socket socket = _serversocket.endaccept(ar);             _clientsockets.add(socket);             //console.writeline("client connected");             socket.beginreceive(_buffer, 0, _buffer.length, socketflags.none, new asynccallback(receivecallback), socket);             _serversocket.beginaccept(new asynccallback(acceptcallback), null);         }          private static void receivecallback(iasyncresult ar)         {             try             {                 socketerror errorcode;                 socket socket = (socket)ar.asyncstate;                 int received = socket.endreceive(ar, out errorcode);                 if (errorcode != socketerror.success)                 {                     received = 0;                 }                  byte[] databuf = new byte[received];                 array.copy(_buffer, databuf, received);                 string text = bitconverter.tostring(databuf);                 string time = datetime.now.timeofday.tostring();                 string dataline = time + "____" + text;                 addtologfile(dataline);                 socket.beginreceive(_buffer, 0, _buffer.length, socketflags.none, new asynccallback(receivecallback), socket);             }             catch (exception ex)             {             }         }          private static void addtologfile(string message)         {             string logpath = @"d:\\logfile\\";             string filename = "log_" + datetime.now.tostring("dd-mm-yyyy") + ".txt";             string filepath = logpath + filename;             if (file.exists(filepath))             {                 using (streamwriter writer = new streamwriter(filepath, true))                 {                     writer.writeline(message);                 }             }             else             {                 streamwriter writer = file.createtext(filepath);                 writer.writeline(message);                 writer.close();             }         }     } } 

now below code every thing working fine

  private static void addtologfile(string message)     {         string logpath = @"d:\logfile\";         string filename = "log_" + datetime.now.tostring("dd-mm-yyyy") + ".txt";         string filepath = logpath + filename;         if (file.exists(filepath))         {             using (streamwriter writer = new streamwriter(filepath, true))             {                 writer.writeline(message);             }         }         else         {             using (streamwriter writer = new streamwriter(filepath, false))             {                using(streamwriter writer1 = file.createtext(filepath))                 {                    if(file.exists(filepath))                    {                        writer.writeline(message);                    }                 }                 //streamwriter writer1 = file.createtext(filepath);                 //writer.writeline(message);             } 

thank guys....


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -