[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”; 无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
石头 编写于 2024-10-10 00:36:00 石头 提交于 2024-10-10 00:45:43
X
using System;
using System.Diagnostics;

namespace NewLife.Log
{
    /// <summary>统计代码的时间消耗</summary>
    public class TimeCost : DisposeBase
    {
        #region 属性
        Stopwatch _sw;

        /// <summary>名称</summary>
        public String Name { get; set; }

        /// <summary>最大时间。毫秒</summary>
        public Int32 Max { get; set; }

        /// <summary>日志输出</summary>
        public ILog Log { get; set; }
        #endregion

        #region 构造
        /// <summary>指定最大执行时间来构造一个代码时间统计</summary>
        /// <param name="name"></param>
        /// <param name="msMax"></param>
        public TimeCost(String name, Int32 msMax = 0)
        {
            Name = name;
            Max = msMax;
            Log = XTrace.Log;

            if (msMax >= 0) Start();
        }

        /// <summary>析构</summary>
        /// <param name="disposing"></param>
        protected override void Dispose(Boolean disposing)
        {
            Stop();

            base.Dispose(disposing);
        }
        #endregion

        #region 方法
        /// <summary>开始</summary>
        public void Start()
        {
            if (_sw == null)
                _sw = Stopwatch.StartNew();
            else if (!_sw.IsRunning)
                _sw.Start();
        }

        /// <summary>停止</summary>
        public void Stop()
        {
            if (_sw != null)
            {
                _sw.Stop();

                if (Log != null && Log != Logger.Null && Log.Enable)
                {
                    var ms = _sw.ElapsedMilliseconds;
                    if (ms > Max)
                    {
                        if (Max > 0)
                            Log.Warn("{0}执行过长警告 {1:n0}ms > {2:n0}ms", Name, ms, Max);
                        else
                            Log.Warn("{0}执行 {1:n0}ms", Name, ms);
                    }
                }
            }
        }
        #endregion
    }
}