Tcp Windows服务没有创建任何日志文件

时间:2016-07-28 08:18:21

标签: c# windows-services background-process tcpclient

我已经创建了一个Windows服务,它将打开端口并将数据记录在文本文件中。它是异步数据通信允许连接多个客户端。但我的问题是它没有创建任何日志文件,也没有记录任何data.kindly指导我在哪里做错了??? 这是我的代码......

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 Up 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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

现在使用以下代码每件事情都运转正常

  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);
            }

谢谢你们......