Tcp windows服务未创建任何日志文件

本文关键字:任何 日志 文件 创建 windows 服务 Tcp | 更新日期: 2023-09-27 17:59:05

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

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

Tcp windows服务未创建任何日志文件

现在有了下面的代码,一切都很好

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

谢谢大家。。。。