C#实现日志

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;

namespace CSharpUtilHelpV2
{
  /// <summary>
  /// 日志类型枚举
  /// </summary>
  public enum LogType
  {
    /// <summary>
    /// 一般输出
    /// </summary>
    Trace,
    /// <summary>
    /// 警告
    /// </summary>
    Warning,
    /// <summary>
    /// 错误
    /// </summary>
    Error,
    /// <summary>
    /// SQL
    /// </summary>
    SQL
  }
  /// <summary>
  /// 基于.NET 2.0日志工具类
  /// </summary>
  public class LogToolV2
  {
    private static readonly Thread LogTask;
    private static readonly ThreadSafeQueueV2<string> LogColQueue;//自定义线程安全的Queue
    private static readonly object SyncRoot;
    private static readonly string FilePath;
    private static readonly long BackFileSize_MB = 2;//超过2M就开始备份日志文件
    static LogToolV2()
    {
      SyncRoot = new object();
      FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Log\\";
      LogTask = new Thread(WriteLog);
      LogColQueue = new ThreadSafeQueueV2<string>();
      LogTask.Start();
      Debug.WriteLine("Log Start......");
    }
    /// <summary>
    /// 记录日志
    /// </summary>
    /// <param name="msg">日志内容</param>
    public static void Log(string msg)
    {
      string _msg = string.Format("{0} : {2}", DateTime.Now.ToString("HH:mm:ss"), msg);
      LogColQueue.Enqueue(msg);
    }
    /// <summary>
    /// 记录日志
    /// </summary>
    /// <param name="msg">日志内容</param>
    /// <param name="type">日志类型</param>
    public static void Log(string msg, LogType type)
    {
      string _msg = string.Format("{0} {1}: {2}", DateTime.Now.ToString("HH:mm:ss"), type, msg);
      LogColQueue.Enqueue(_msg);
    }
    /// <summary>
    /// 记录日志
    /// </summary>
    /// <param name="ex">异常</param>
    public static void Log(Exception ex)
    {
      if (ex != null)
      {
        string _newLine = Environment.NewLine;
        StringBuilder _builder = new StringBuilder();
        _builder.AppendFormat("{0}: {1}{2}", DateTime.Now.ToString("HH:mm:ss"), ex.Message, _newLine);
        _builder.AppendFormat("{0}{1}", ex.GetType(), _newLine);
        _builder.AppendFormat("{0}{1}", ex.Source, _newLine);
        _builder.AppendFormat("{0}{1}", ex.TargetSite, _newLine);
        _builder.AppendFormat("{0}{1}", ex.StackTrace, _newLine);
        LogColQueue.Enqueue(_builder.ToString());
      }
    }
    private static void WriteLog()
    {
      while (true)
      {
        if (LogColQueue.Count() > 0)
        {
          string _msg = LogColQueue.Dequeue();
          Monitor.Enter(SyncRoot);
          if (!CreateDirectory()) continue;
          string _path = string.Format("{0}{1}.log", FilePath, DateTime.Now.ToString("yyyyMMdd"));
          Monitor.Exit(SyncRoot);
          lock (SyncRoot)
          {
            if (CreateFile(_path))
              ProcessWriteLog(_path, _msg);//写入日志到文本
          }
          ProcessBackLog(_path);//日志备份
        }
      }
    }
    private static void ProcessBackLog(string path)
    {
      lock (SyncRoot)
      {
        if (FileToolV2.GetMBSize(path) > BackFileSize_MB)
        {
          FileToolV2.CopyToBak(path);
        }
      }
    }
    private static void ProcessWriteLog(string path, string msg)
    {
      try
      {
        StreamWriter _sw = File.AppendText(path);
        _sw.WriteLine(msg);
        _sw.Flush();
        _sw.Close();
      }
      catch (Exception ex)
      {
        Debug.WriteLine(string.Format("写入日志失败,原因:{0}", ex.Message));
      }
    }
    private static bool CreateFile(string path)
    {
      bool _result = true;
      try
      {
        if (!File.Exists(path))
        {
          FileStream _files = File.Create(path);
          _files.Close();
        }
      }
      catch (Exception)
      {
        _result = false;
      }
      return _result;
    }
    private static bool CreateDirectory()
    {
      bool _result = true;
      try
      {
        if (!Directory.Exists(FilePath))
        {
          Directory.CreateDirectory(FilePath);
        }
      }
      catch (Exception)
      {
        _result = false;
      }
      return _result;
    }

  }
}
using CSharpUtilHelpV2;
using System;
using System.Diagnostics;
using System.Threading;

namespace LogUtilHelpV2Test
{
  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        Debug.WriteLine("-------------");
        Action _writeLog = delegate()
        {
          for (int i = 0; i < 10000; i++)
            LogToolV2.Log(Guid.NewGuid().ToString(), LogType.Trace);
        };
        Thread _wireteLogTask1 = new Thread(new ThreadStart(_writeLog));
        _wireteLogTask1.Start();

        Thread _wireteLogTask2 = new Thread(new ThreadStart(_writeLog));
        _wireteLogTask2.Start();

        //throw new Exception("test  aaa bb cc");
      }
      catch (Exception ex)
      {
        LogToolV2.Log(ex);
        Console.WriteLine(ex.Message.Trim());
      }
      finally
      {
        Console.WriteLine("ok");
        Console.ReadLine();
      }
    }
  }
}
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
    // Lists all available networks
    Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
    foreach ( Wlan.WlanAvailableNetwork network in networks )
    {                     
        Console.WriteLine( "Found network with SSID {0}.", GetStringForSSID(network.dot11Ssid));
    }
}

static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
    return Encoding.ASCII.GetString( ssid.SSID, 0, (int) ssid.SSIDLength );
}

RSSI (or "Radio (Received) Signal Strength Indicator") are in units of 'dB' (decibel) or the similar 'dBm' (dB per milliwatt) (See dB vs. dBm) in which the smaller magnitude negative numbers have the highest signal strength, or quality.


Therefore, the conversion between quality (percentage) and dBm is as follows:
    quality = 2 * (dBm + 100)  where dBm: [-100 to -50]

    dBm = (quality / 2) - 100  where quality: [0 to 100]


Pseudo Code (with example clamping):
    // dBm to Quality:
    if(dBm <= -100)
        quality = 0;
    else if(dBm >= -50)
        quality = 100;
    else
        quality = 2 * (dBm + 100);

    // Quality to dBm:
    if(quality <= 0)
        dBm = -100;
    else if(quality >= 100)
        dBm = -50;
    else
        dBm = (quality / 2) - 100;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值