92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace MQTTServerSideAPI
|
|
{
|
|
public static class Logger
|
|
{
|
|
public static void LogSide(string message, string side) {
|
|
string logFilePath = GetLogFile(side);
|
|
using (StreamWriter sw = new StreamWriter(logFilePath, true)) {
|
|
sw.WriteLine($"{DateTime.Now.ToString()} - {message}");
|
|
sw.Close();
|
|
}
|
|
}
|
|
public static string GetLogFile(string side)
|
|
{
|
|
string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
|
|
if (!Directory.Exists(logPath))
|
|
{
|
|
Directory.CreateDirectory(logPath);
|
|
}
|
|
|
|
//string logFilePath = Path.Combine(logPath, $"{DateTime.Now.ToString("yyyyMMdd")}_{side}.log");
|
|
string logFilePath = Path.Combine(logPath, $"mqtttest_{side}.log");
|
|
return logFilePath;
|
|
}
|
|
public static void LogCallinSide(string message)
|
|
{
|
|
LogSide(message, "callin");
|
|
}
|
|
public static void LogCalloutanaSide(string message)
|
|
{
|
|
LogSide(message, "calloutanalysis");
|
|
}
|
|
public static void LogDownSide(string message)
|
|
{
|
|
LogSide(message, "down");
|
|
}
|
|
public static List<string> GetAllCallin()
|
|
{
|
|
List<string> listRet = new List<string>();
|
|
string logFilePath = GetLogFile("callin");
|
|
if (!File.Exists(logFilePath))
|
|
{
|
|
return listRet;
|
|
}
|
|
|
|
try
|
|
{
|
|
using (StreamReader sr = new StreamReader(logFilePath, true))
|
|
{
|
|
while (sr.Peek() >= 0)
|
|
{
|
|
listRet.Add(sr.ReadLine());
|
|
}
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
System.Diagnostics.EventLog.WriteEntry("MQTTServerSideAPI", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss fff") + " " + ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
|
|
}
|
|
|
|
return listRet;
|
|
}
|
|
|
|
public static List<string> GetFilterCallin(string productid, string devicename)
|
|
{
|
|
//1. get all
|
|
//2. filter
|
|
List<string> alllogs = GetAllCallin();
|
|
|
|
IEnumerable<string> filterExp = alllogs.Where((log) => {
|
|
String[] seps = { " - " };
|
|
string[] part2arr = log.Split(seps, StringSplitOptions.RemoveEmptyEntries);
|
|
if(part2arr.Length > 1)
|
|
{
|
|
if (part2arr[1].StartsWith("productid:" + productid + ",devicename:" + devicename + ","))
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
|
|
return filterExp.ToList();
|
|
}
|
|
}
|
|
} |