增加 log 机制

This commit is contained in:
jiahao 2023-10-30 08:54:54 +08:00
parent c41d079396
commit df2654fb57
9 changed files with 202 additions and 151 deletions

View File

@ -486,9 +486,9 @@ namespace SolarPower.Controllers
vForDataCount++; vForDataCount++;
object vv = ""; object vv = "";
avghour = Convert.ToString( dbody["tothour"]) ?? "NaN"; avghour = (dbody["tothour"] == null) ? "NaN" : dbody["tothour"].ToString();
avgKWHKWP = Convert.ToString(dbody["KWHKWP"]) ?? "NaN"; avgKWHKWP = (dbody["KWHKWP"] == null) ? "NaN" : dbody["KWHKWP"].ToString();
avgdayKWH = Convert.ToString(dbody["dayKWH"]) ?? "NaN"; avgdayKWH = (dbody["dayKWH"] == null) ? "NaN" : dbody["dayKWH"].ToString();
//if (dbody.TryGetValue("tothour", out vv)) //if (dbody.TryGetValue("tothour", out vv))
// avghour = (Convert.ToDouble(dbody["tothour"].ToString()) + Convert.ToDouble(avghour)).ToString(); // avghour = (Convert.ToDouble(dbody["tothour"].ToString()) + Convert.ToDouble(avghour)).ToString();

View File

@ -64,14 +64,20 @@ namespace solarApp
// Console.WriteLine("Error Occured"); // Console.WriteLine("Error Occured");
// } // }
// } // }
// ref: https://www.ruyut.com/2021/10/serilog.html
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose() // 設定最低顯示層級 預設: Information .MinimumLevel.Verbose() // 設定最低顯示層級 預設: Information
.WriteTo.File("Logs/solarApp/log-.log", .WriteTo.File("Logs/log-.log",
rollingInterval: RollingInterval.Day, // 每天一個檔案 rollingInterval: RollingInterval.Day, // 每天一個檔案
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u5}] {Message:lj}{NewLine}{Exception}" outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u5}] {Message:lj}{NewLine}{Exception}"
) // 輸出到檔案 檔名範例: log-20211005.log ) // 輸出到檔案 檔名範例: log-20211005.log
.CreateLogger(); .CreateLogger();
//Log.Information("Init log");
// Log.Debug("debug");
Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);

View File

@ -5,15 +5,16 @@ using MySql.Data.MySqlClient;
using solarApp.Model; using solarApp.Model;
using System.Configuration; using System.Configuration;
using Dapper; using Dapper;
using Microsoft.Extensions.Logging; //using Microsoft.Extensions.Logging;
using System.Windows.Forms; using System.Windows.Forms;
using Serilog;
namespace solarApp.Service namespace solarApp.Service
{ {
class archiveLowData class archiveLowData
{ {
string Connection1 = string.Empty; string Connection1 = string.Empty;
private ILogger logger = Log.ForContext<archiveLowData>();
public archiveLowData(string Connection_parame = null) public archiveLowData(string Connection_parame = null)
{ {
if (!string.IsNullOrEmpty(Connection_parame)) if (!string.IsNullOrEmpty(Connection_parame))
@ -42,8 +43,9 @@ namespace solarApp.Service
// string yyyymm = System.DateTime.Now.AddMonths(-2).ToString("yyyy-MM"); // string yyyymm = System.DateTime.Now.AddMonths(-2).ToString("yyyy-MM");
_siteID = siteID; _siteID = siteID;
//搬移的月份 //搬移的月份
move_month = System.DateTime.Now.AddMonths(-1).ToString("yyyy-MM")+ "-01"; // move_month = System.DateTime.Now.AddMonths(-1).ToString("yyyy-MM")+ "-01";
// move_month = System.DateTime.Now.ToString("yyyy-MM") + "--01"; //這個月 move_month = System.DateTime.Now.ToString("yyyy-MM") + "-01"; //這個月
//_date1 = date1; //_date1 = date1;
get_siteInfo(); get_siteInfo();
checkTable(); checkTable();
@ -51,8 +53,6 @@ namespace solarApp.Service
moveData_station(); moveData_station();
moveData_inv(); moveData_inv();
moveData_sensor(); moveData_sensor();
result = true; result = true;
return result; return result;
@ -301,7 +301,7 @@ namespace solarApp.Service
//System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_station]的資料表向[{_siteDB}_history.s{_siteID}_station]搬移"); //System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】開始執行[{_siteDB}.s{_siteID}_station]的資料表向[{_siteDB}_history.s{_siteID}_station]搬移");
string for_insert_station = $@"INSERT {_siteDB}_history.s{_siteID01}_station(`ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `insertTime`) string for_insert_station = $@"INSERT {_siteDB}_history.s{_siteID01}_station(`ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `insertTime`)
SELECT `ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `insertTime` FROM {_siteDB}.s{_siteID01}_station SELECT `ID`, `TIMESTAMP`, `SITEID`, `SITETYPE`, `CONTROLLERID`, `KWH`, `TODAYKWH`, `TOTALKWH`, `KWHKWP`, `PR`, `MP`, `SOLARHOUR`, `insertTime` FROM {_siteDB}.s{_siteID01}_station
WHERE FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') < '{move_month}'"; WHERE FROM_UNIXTIME(`TIMESTAMP`/1000, '%Y-%m-%d') < '{move_month}'";
int rowCT = conn.Execute(for_insert_station, commandTimeout : 600); int rowCT = conn.Execute(for_insert_station, commandTimeout : 600);
// System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_station的七月前資料寫入至{_siteDB}_history.s{_siteID}_station【寫入成功】於{dateTime}"); // System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_station的七月前資料寫入至{_siteDB}_history.s{_siteID}_station【寫入成功】於{dateTime}");
@ -315,6 +315,8 @@ namespace solarApp.Service
//刪除原本的資料 //刪除原本的資料
string for_delete_station = $@"DELETE FROM {_siteDB}.s{_siteID01}_station string for_delete_station = $@"DELETE FROM {_siteDB}.s{_siteID01}_station
WHERE FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') < '{move_month}'"; WHERE FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') < '{move_month}'";
//string for_delete_station = $@"DELETE FROM {_siteDB}.s{_siteID01}_station
// WHERE date(CrdTime) < '{move_month}'";
rowCT = conn.Execute(for_delete_station, commandTimeout: 600); rowCT = conn.Execute(for_delete_station, commandTimeout: 600);
//System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_station.ToString()} 筆"); //System.Diagnostics.Debug.WriteLine($"【ArchiveRowData】{_siteDB}.s{_siteID}_sensoravg搬移至{_siteDB}_history.s{_siteID}_sensoravg【搬移資料成功】於{dateTime} 共 {delete_station.ToString()} 筆");
duration = DateTime.Now - dt_start1; duration = DateTime.Now - dt_start1;

View File

@ -5,6 +5,7 @@ using MySql.Data.MySqlClient;
using Dapper; using Dapper;
using solarApp.Model; using solarApp.Model;
using System.Configuration; using System.Configuration;
using Serilog;
namespace solarApp.Service namespace solarApp.Service
{ {
@ -14,6 +15,9 @@ namespace solarApp.Service
public class getStationSvc public class getStationSvc
{ {
public string Connection1 { get; set; } public string Connection1 { get; set; }
private ILogger logger = Log.ForContext<getSensorSvc>();
public getStationSvc(string Connection_parame = null) public getStationSvc(string Connection_parame = null)
{ {
if (!string.IsNullOrEmpty(Connection_parame)) if (!string.IsNullOrEmpty(Connection_parame))
@ -37,7 +41,7 @@ namespace solarApp.Service
/// <returns></returns> /// <returns></returns>
public List<raw_statino> get_station_raw(string reportDate, string siteDB, string siteID) public List<raw_statino> get_station_raw(string reportDate, string siteDB, string siteID)
{ {
List<raw_statino> ds; List<raw_statino> ds = null;
using (MySqlConnection conn = new MySqlConnection(Connection1)) using (MySqlConnection conn = new MySqlConnection(Connection1))
{ {
conn.Open(); conn.Open();
@ -52,7 +56,7 @@ namespace solarApp.Service
} }
catch (Exception ex) catch (Exception ex)
{ {
throw ex; logger.Error(@$"【get_station_raw】執行失敗 {siteDB}.s{siteID} _station 在{reportDate} - [Exception]" + ex.ToString());
} }
return ds; return ds;
} }
@ -104,7 +108,7 @@ namespace solarApp.Service
} }
catch (Exception ex) catch (Exception ex)
{ {
throw ex; logger.Error(@$"【get_station_rawAvg】執行失敗 {siteDB}.s{siteID} _station 在{date1} - {date2} - [Exception]" + ex.ToString());
} }
return ds; return ds;

View File

@ -6,7 +6,8 @@ using Dapper;
using solarApp.Model; using solarApp.Model;
using System.Configuration; using System.Configuration;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; //using Microsoft.Extensions.Logging;
using Serilog;
namespace solarApp.Service namespace solarApp.Service
{ {
@ -14,6 +15,18 @@ namespace solarApp.Service
{ {
string Connection1 = string.Empty; string Connection1 = string.Empty;
ILogger _logger; ILogger _logger;
public ILogger MyLoger
{
get
{
if (_logger == null)
{
//需要添加使用的当前类名空间名称,不加的话,不显示
_logger = Log.ForContext<procArchiveHourly>();
}
return _logger;
}
}
public string _siteID { get; set; } public string _siteID { get; set; }
public string _siteDB { get; set; } public string _siteDB { get; set; }
public string _siteID01 { get; set; } public string _siteID01 { get; set; }
@ -44,7 +57,7 @@ namespace solarApp.Service
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】開始執行[{0}]在{1}取得電站編號", _siteID, _date1); _logger.Information("【ProcArchiveSensorHourly】開始執行[{0}]在{1}取得電站編號", _siteID, _date1);
} }
#region PowerStationID #region PowerStationID
@ -60,7 +73,7 @@ namespace solarApp.Service
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】執行完成[{0}]在{1}取得電站編號 - {2}", _siteID, _date1, _powerStationID); _logger.Information("【ProcArchiveSensorHourly】執行完成[{0}]在{1}取得電站編號 - {2}", _siteID, _date1, _powerStationID);
} }
#endregion #endregion
@ -72,8 +85,8 @@ namespace solarApp.Service
{ {
if (_logger != null) if (_logger != null)
{ {
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}取得電站編號", _siteID, _date1); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}取得電站編號", _siteID, _date1);
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}取得電站編號 - [Exception]{2}", _siteID, _date1, ex.ToString()); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}取得電站編號 - [Exception]{2}", _siteID, _date1, ex.ToString());
} }
throw ex; throw ex;
@ -95,7 +108,7 @@ namespace solarApp.Service
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】開始執行[{0}]在{1}設備寫入資料表的資料", _siteID, _date1); _logger.Information("【ProcArchiveSensorHourly】開始執行[{0}]在{1}設備寫入資料表的資料", _siteID, _date1);
} }
string sql = $@" string sql = $@"
select x.*, z.itemname, z.HourType, z.DayType, z.MonthType,z.SetWhat select x.*, z.itemname, z.HourType, z.DayType, z.MonthType,z.SetWhat
@ -161,7 +174,7 @@ namespace solarApp.Service
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】執行完成[{0}]在{1}設備寫入資料表的資料", _siteID, _date1); _logger.Information("【ProcArchiveSensorHourly】執行完成[{0}]在{1}設備寫入資料表的資料", _siteID, _date1);
} }
conn.Close(); conn.Close();
@ -172,8 +185,8 @@ namespace solarApp.Service
{ {
if (_logger != null) if (_logger != null)
{ {
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備寫入資料表的資料", _siteID, _date1); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備寫入資料表的資料", _siteID, _date1);
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備寫入資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString()); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備寫入資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString());
} }
throw ex; throw ex;
} }
@ -209,8 +222,8 @@ namespace solarApp.Service
{ {
if (_logger != null) if (_logger != null)
{ {
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}日照計更新至資料表的資料", _siteID, _date1); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}日照計更新至資料表的資料", _siteID, _date1);
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}日照計更新至資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString()); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}日照計更新至資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString());
} }
throw ex; throw ex;
} }
@ -233,7 +246,7 @@ namespace solarApp.Service
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】開始執行[{0}]在{1}累積日照差更新至資料表的資料", _siteID, _date1); _logger.Information("【ProcArchiveSensorHourly】開始執行[{0}]在{1}累積日照差更新至資料表的資料", _siteID, _date1);
} }
//取前累積日照 //取前累積日照
string sql = $@" select IrrDay string sql = $@" select IrrDay
@ -281,7 +294,7 @@ namespace solarApp.Service
conn.Execute(ss, commandTimeout: 600); conn.Execute(ss, commandTimeout: 600);
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】執行完成[{0}]在{1}累積日照差更新至資料表的資料", _siteID, _date1); _logger.Information("【ProcArchiveSensorHourly】執行完成[{0}]在{1}累積日照差更新至資料表的資料", _siteID, _date1);
} }
conn.Close(); conn.Close();
} }
@ -290,8 +303,8 @@ namespace solarApp.Service
{ {
if (_logger != null) if (_logger != null)
{ {
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}累積日照差更新資料表的資料", _siteID, _date1); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}累積日照差更新資料表的資料", _siteID, _date1);
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}累積日照差更新資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString()); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}累積日照差更新資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString());
} }
throw ex; throw ex;
} }
@ -309,7 +322,7 @@ namespace solarApp.Service
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】開始執行[{0}]在{1}設備清除資料表的資料", _siteID, _date1); _logger.Information("【ProcArchiveSensorHourly】開始執行[{0}]在{1}設備清除資料表的資料", _siteID, _date1);
} }
string d1 = _date1 + ":00:00"; string d1 = _date1 + ":00:00";
string clearTime = Convert.ToDateTime(d1).ToString("yyyy-MM-dd"); string clearTime = Convert.ToDateTime(d1).ToString("yyyy-MM-dd");
@ -325,7 +338,7 @@ namespace solarApp.Service
if (_logger != null) if (_logger != null)
{ {
_logger.LogInformation("【ProcArchiveSensorHourly】執行完成[{0}]在{1}設備清除資料表的資料", _siteID, _date1); _logger.Information("【ProcArchiveSensorHourly】執行完成[{0}]在{1}設備清除資料表的資料", _siteID, _date1);
} }
conn.Close(); conn.Close();
@ -336,8 +349,8 @@ namespace solarApp.Service
{ {
if (_logger != null) if (_logger != null)
{ {
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備清除資料表的資料", _siteID, _date1); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備清除資料表的資料", _siteID, _date1);
_logger.LogError("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備清除資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString()); _logger.Error("【ProcArchiveSensorHourly】執行失敗[{0}]在{1}設備清除資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString());
} }
throw ex; throw ex;
} }

View File

@ -6,7 +6,9 @@ using Dapper;
using solarApp.Model; using solarApp.Model;
using System.Configuration; using System.Configuration;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging; //using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
//using solarApp.Service.proArchiveLog; //using solarApp.Service.proArchiveLog;
namespace solarApp.Service namespace solarApp.Service
@ -14,10 +16,10 @@ namespace solarApp.Service
public class procInvSvc public class procInvSvc
{ {
string Connection1 = string.Empty; string Connection1 = string.Empty;
ILogger _logger; private ILogger logger = Log.ForContext<procInvSvc>();
public procInvSvc(string Connection_parame = null, ILogger logger = null) public procInvSvc(string Connection_parame = null)
{ {
if (!string.IsNullOrEmpty(Connection_parame)) if (!string.IsNullOrEmpty(Connection_parame))
{ {
@ -28,10 +30,10 @@ namespace solarApp.Service
Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString;
} }
if (logger != null) //if (logger != null)
{ //{
_logger = logger; // _logger = logger;
} //}
} }
public string _siteID { get; set; } public string _siteID { get; set; }
@ -52,10 +54,10 @@ namespace solarApp.Service
conn.Open(); conn.Open();
try try
{ {
if (_logger != null) //if (_logger != null)
{ //{
_logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1); // logger.Information("【ProcInvSvc】開始執行[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1);
} //}
//string sql = @" //string sql = @"
// delete from inverter_history_15min where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; // delete from inverter_history_15min where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1;
@ -77,19 +79,14 @@ namespace solarApp.Service
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); cmd.Parameters.Clear();
if (_logger != null) logger.Information(@$"【ProcInvSvc】執行完成[{_siteID}]在 {_date1} 逆變器清除資料表的資料");
{
_logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1);
}
result = true; result = true;
} }
catch (Exception ex) catch (Exception ex)
{ {
if (_logger != null)
{ logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1);
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器清除資料表的資料", _siteID, _date1); logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器清除資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString());
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器清除資料表的資料 - [Exception]{2}", _siteID, _date1, ex.ToString());
}
//throw ex; //throw ex;
arclog.insert_log(_powerStationID, "step0 del inv ", 0, "inv", "del inv error", "0", "", "", conn, cmd); arclog.insert_log(_powerStationID, "step0 del inv ", 0, "inv", "del inv error", "0", "", "", conn, cmd);
@ -137,10 +134,8 @@ namespace solarApp.Service
conn.Open(); conn.Open();
try try
{ {
if (_logger != null)
{ logger.Information("【ProcInvSvc】開始執行[{0}]在{1}逆變器取得電站編號", _siteID, _date1);
_logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器取得電站編號", _siteID, _date1);
}
#region PowerStationID #region PowerStationID
string sql = @" select id , `code` siteID, siteDB, `name` siteName string sql = @" select id , `code` siteID, siteDB, `name` siteName
@ -153,10 +148,8 @@ namespace solarApp.Service
_siteID01 = item.SiteID + "01"; _siteID01 = item.SiteID + "01";
} }
if (_logger != null)
{ logger.Information("【ProcInvSvc】執行完成[{0}]在{1}逆變器取得電站編號 - {2}", _siteID, _date1, _powerStationID);
_logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器取得電站編號 - {2}", _siteID, _date1, _powerStationID);
}
#endregion #endregion
@ -164,11 +157,10 @@ namespace solarApp.Service
} }
catch (Exception ex) catch (Exception ex)
{ {
if (_logger != null)
{ logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器取得電站編號", _siteID, _date1);
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器取得電站編號", _siteID, _date1); logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器取得電站編號 - [Exception]{2}", _siteID, _date1, ex.ToString());
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器取得電站編號 - [Exception]{2}", _siteID, _date1, ex.ToString());
}
arclog.insert_log(_powerStationID, "step0 select stationID ", 0, "stationID", "select stationID", "0", "", "", conn, null); arclog.insert_log(_powerStationID, "step0 select stationID ", 0, "stationID", "select stationID", "0", "", "", conn, null);
} }
conn.Close(); conn.Close();
@ -191,10 +183,7 @@ namespace solarApp.Service
#region 15 min #region 15 min
try try
{ {
if (_logger != null) logger.Information("【ProcInvSvc】開始執行[{0}]在{1}逆變器15min補償", _siteID, _date1);
{
_logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器15min補償", _siteID, _date1);
}
sql = @$"DROP TABLE IF EXISTS del_Inv_day_" + _powerStationID + @"_s1; sql = @$"DROP TABLE IF EXISTS del_Inv_day_" + _powerStationID + @"_s1;
CREATE TABLE del_Inv_day_" + _powerStationID + @"_s1 CREATE TABLE del_Inv_day_" + _powerStationID + @"_s1
@ -248,17 +237,13 @@ namespace solarApp.Service
cmd.Parameters.Clear(); cmd.Parameters.Clear();
cmd.Dispose(); cmd.Dispose();
if (_logger != null)
_logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器15min補償", _siteID, _date1); logger.Information("【ProcInvSvc】執行完成[{0}]在{1}逆變器15min補償", _siteID, _date1);
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器15min補償", _siteID, _date1);
if (_logger != null) logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器15min補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
{
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器15min補償", _siteID, _date1);
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器15min補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
}
arclog.insert_log(_powerStationID, "step1 insert inverter_15min history " + _date1, 0, "inverter_history_15min", "insert inverter_15min history", "1", "", "", conn, cmd); arclog.insert_log(_powerStationID, "step1 insert inverter_15min history " + _date1, 0, "inverter_history_15min", "insert inverter_15min history", "1", "", "", conn, cmd);
} }
#endregion #endregion
@ -266,8 +251,7 @@ namespace solarApp.Service
#region hour #region hour
try try
{ {
if (_logger != null) logger.Information("【ProcInvSvc】開始執行[{0}]在{1}逆變器hour補償", _siteID, _date1);
_logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器hour補償", _siteID, _date1);
sql = @$"DROP TEMPORARY TABLE IF EXISTS Inv_day_" + _powerStationID + @"_s2; sql = @$"DROP TEMPORARY TABLE IF EXISTS Inv_day_" + _powerStationID + @"_s2;
CREATE TEMPORARY TABLE Inv_day_" + _powerStationID + @"_s2 CREATE TEMPORARY TABLE Inv_day_" + _powerStationID + @"_s2
@ -323,8 +307,7 @@ namespace solarApp.Service
//ct = conn.Execute(sql, new { date1 = _date1 }); //ct = conn.Execute(sql, new { date1 = _date1 });
cmd.CommandText = sql; cmd.CommandText = sql;
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
if (_logger != null) logger.Information("【ProcInvSvc】執行完成[{0}]在{1}逆變器hour補償", _siteID, _date1);
_logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器hour補償", _siteID, _date1);
sql = @$"DROP TABLE IF EXISTS del_Inv_day_" + _powerStationID + @"_s1; "; sql = @$"DROP TABLE IF EXISTS del_Inv_day_" + _powerStationID + @"_s1; ";
//conn.Execute(ss); //conn.Execute(ss);
@ -333,11 +316,9 @@ namespace solarApp.Service
} }
catch (Exception ex) catch (Exception ex)
{ {
if (_logger != null)
{ logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器hour補償", _siteID, _date1);
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器hour補償", _siteID, _date1); logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器hour補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器hour補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
}
arclog.insert_log(_powerStationID, "inverter_history_hour step1 " + _date1, 0, "inverter_history_hour", "insert inverter_history_hour error", "0", "", "", conn, cmd); arclog.insert_log(_powerStationID, "inverter_history_hour step1 " + _date1, 0, "inverter_history_hour", "insert inverter_history_hour error", "0", "", "", conn, cmd);
// throw ex; // throw ex;
} }
@ -347,10 +328,8 @@ namespace solarApp.Service
#region day #region day
try try
{ {
if (_logger != null)
{ logger.Information("【ProcInvSvc】開始執行[{0}]在{1}逆變器day補償", _siteID, _date1);
_logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器day補償", _siteID, _date1);
}
sql = @" sql = @"
INSERT INTO solar_master.inverter_history_day(PowerStationId, INVERTERID, TIMESTAMP, Irradiance, IrrDay, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH, INSERT INTO solar_master.inverter_history_day(PowerStationId, INVERTERID, TIMESTAMP, Irradiance, IrrDay, AC1V, AC1A, AC1W, AC1F, AC1WH, AC2V, AC2A, AC2W, AC2F, AC2WH,
@ -394,16 +373,14 @@ namespace solarApp.Service
cmd.Parameters.Clear(); cmd.Parameters.Clear();
cmd.Dispose(); cmd.Dispose();
if (_logger != null)
_logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器day補償", _siteID, _date1); logger.Information("【ProcInvSvc】執行完成[{0}]在{1}逆變器day補償", _siteID, _date1);
} }
catch (Exception ex) catch (Exception ex)
{ {
if (_logger != null)
{ logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器day補償", _siteID, _date1);
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器day補償", _siteID, _date1); logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器day補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器day補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
}
//throw ex; //throw ex;
arclog.insert_log(_powerStationID, "inverter_history_day step1 ", 0, "inverter_history_day", "insert inverter_history_day error", "0", "", ex.Message, conn, cmd); arclog.insert_log(_powerStationID, "inverter_history_day step1 ", 0, "inverter_history_day", "insert inverter_history_day error", "0", "", ex.Message, conn, cmd);
} }
@ -412,8 +389,8 @@ namespace solarApp.Service
#region month #region month
try try
{ {
if (_logger != null)
_logger.LogInformation("【ProcInvSvc】開始執行[{0}]在{1}逆變器month補償", _siteID, _date1); logger.Information("【ProcInvSvc】開始執行[{0}]在{1}逆變器month補償", _siteID, _date1);
sql = @" sql = @"
delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ; delete from inverter_history_month where powerstationID = @powerStationID and left(`TIMESTAMP`, 7) = left(@date1, 7) ;
@ -458,18 +435,14 @@ namespace solarApp.Service
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); cmd.Parameters.Clear();
if (_logger != null)
{ logger.Information("【ProcInvSvc】執行完成[{0}]在{1}逆變器month補償", _siteID, _date1);
_logger.LogInformation("【ProcInvSvc】執行完成[{0}]在{1}逆變器month補償", _siteID, _date1);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
if (_logger != null)
{ logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器month補償", _siteID, _date1);
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器month補償", _siteID, _date1); logger.Error("【ProcInvSvc】執行失敗[{0}]在{1}逆變器month補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
_logger.LogError("【ProcInvSvc】執行失敗[{0}]在{1}逆變器month補償 - [Exception]{2}", _siteID, _date1, ex.ToString());
}
arclog.insert_log(_powerStationID, "inverter_history_month step1 ", 0, "inverter_history_month", "insert inverter_history_month error", "0", "", ex.Message, conn, cmd); arclog.insert_log(_powerStationID, "inverter_history_month step1 ", 0, "inverter_history_month", "insert inverter_history_month error", "0", "", ex.Message, conn, cmd);
// throw ex; // throw ex;
} }
@ -513,10 +486,8 @@ namespace solarApp.Service
#region get SQL #region get SQL
//try //try
//{ //{
if (_logger != null)
{ logger.Information("【Proc_inv_日報 】開始執行[{0}]在{1}inv_get_SQLStatement", _siteID, _date1);
_logger.LogInformation("【Proc_inv_日報 】開始執行[{0}]在{1}inv_get_SQLStatement", _siteID, _date1);
}
arclog.insert_log(_powerStationID, "report_day step > table 開始建立", (DateTime.Now - dt_start).TotalSeconds, "inverter_history_hour", "temp_invDay", "0", "", "日報 開始建立table", conn, cmd); arclog.insert_log(_powerStationID, "report_day step > table 開始建立", (DateTime.Now - dt_start).TotalSeconds, "inverter_history_hour", "temp_invDay", "0", "", "日報 開始建立table", conn, cmd);
#region temp table #region temp table
sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1; sql = @$"DROP TABLE IF EXISTS temp_invDay{_powerStationID}_s1;
@ -529,7 +500,7 @@ namespace solarApp.Service
ALTER TABLE `temp_invDay{_powerStationID}_s1` ADD INDEX `temp_indexs1` (report_date, inverterid);"; ALTER TABLE `temp_invDay{_powerStationID}_s1` ADD INDEX `temp_indexs1` (report_date, inverterid);";
//conn.Execute(ss); //conn.Execute(ss);
arclog.insert_log(_powerStationID, "report_day step > table 建立完成", (DateTime.Now - dt_start).TotalSeconds, "inverter_history_hour", "temp_invDay", "1", "", "日報 建立table完成", conn, cmd); arclog.insert_log(_powerStationID, "report_day step > table 建立完成 " + _date1, (DateTime.Now - dt_start).TotalSeconds, "inverter_history_hour", "temp_invDay", "1", "", "日報 建立table完成", conn, cmd);
try try
{ {
@ -541,7 +512,7 @@ namespace solarApp.Service
var rowCT = cmd.ExecuteScalar(); var rowCT = cmd.ExecuteScalar();
//select LAST_INSERT_ID() //select LAST_INSERT_ID()
TimeSpan duration = DateTime.Now - dt_start; TimeSpan duration = DateTime.Now - dt_start;
arclog.insert_log(_powerStationID, "report_day step1", duration.TotalSeconds, "inverter_history_hour", "temp_invDay", "1", "", rowCT.ToString(), conn, cmd); arclog.insert_log(_powerStationID, "report_day step1 " + _date1, duration.TotalSeconds, "inverter_history_hour", "temp_invDay", "1", "", rowCT.ToString(), conn, cmd);
} }
catch (Exception ex) catch (Exception ex)
@ -666,8 +637,7 @@ set a.inv_02 = b.inv_02 ");
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
if (_logger != null) logger.Information("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1);
_logger.LogInformation("【Proc_inv_日報 table 】執行完成[{0}]在{1} inv_get_SQLStatement", _siteID, _date1);
TimeSpan duration = DateTime.Now - dt_start3; TimeSpan duration = DateTime.Now - dt_start3;
arclog.insert_log(_powerStationID, "report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", "1", "", rowCT.ToString(), conn, cmd); arclog.insert_log(_powerStationID, "report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", "1", "", rowCT.ToString(), conn, cmd);
@ -677,11 +647,9 @@ set a.inv_02 = b.inv_02 ");
{ {
TimeSpan duration = DateTime.Now - dt_start3 ; TimeSpan duration = DateTime.Now - dt_start3 ;
arclog.insert_log(_powerStationID, "report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", "0", "", ex.Message, conn, cmd); arclog.insert_log(_powerStationID, "report_day step3 update inv02", duration.TotalSeconds, "inverter_history_hour", "report_invday", "0", "", ex.Message, conn, cmd);
if (_logger != null)
{ logger.Error("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement", _siteID, _date1);
_logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement", _siteID, _date1); logger.Error("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement - [Exception]{2}", _siteID, _date1, ex.ToString());
_logger.LogError("【Proc_inv_日報 table】執行失敗[{0}]在{1}inv_get_SQLStatement - [Exception]{2}", _siteID, _date1, ex.ToString());
}
} }
#endregion #endregion

View File

@ -9,6 +9,8 @@ using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
using MySqlX.XDevAPI.Relational; using MySqlX.XDevAPI.Relational;
using static solarApp.Model.weather_model; using static solarApp.Model.weather_model;
using Serilog;
using Serilog.Events;
namespace solarApp.Service namespace solarApp.Service
{ {
@ -16,6 +18,8 @@ namespace solarApp.Service
{ {
string Connection1 = string.Empty; string Connection1 = string.Empty;
private ILogger logger = Log.ForContext<procSensorSvc>();
public procSensorSvc(string Connection_parame = null) public procSensorSvc(string Connection_parame = null)
{ {
if (!string.IsNullOrEmpty(Connection_parame)) if (!string.IsNullOrEmpty(Connection_parame))
@ -136,12 +140,14 @@ namespace solarApp.Service
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); cmd.Parameters.Clear();
conn.Close(); conn.Close();
logger.Information("sensor_history clearData OK");
} }
result = true; result = true;
} }
catch (Exception ex) catch (Exception ex)
{ {
//throw ex; //throw ex;
logger.Error("sensor_history error: " + ex.Message);
} }
return result; return result;
} }
@ -294,6 +300,7 @@ namespace solarApp.Service
//cmd.Parameters() //cmd.Parameters()
//cmd.ExecuteNonQuery(); //cmd.ExecuteNonQuery();
//conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 }); //conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 });
DateTime dt_start2 = DateTime.Now;
cmd.Connection = conn; cmd.Connection = conn;
cmd.CommandTimeout = 720; cmd.CommandTimeout = 720;
@ -302,8 +309,8 @@ namespace solarApp.Service
cmd.CommandText = sql; cmd.CommandText = sql;
int rowCT = cmd.ExecuteNonQuery(); int rowCT = cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); cmd.Parameters.Clear();
DateTime dt_start2 = DateTime.Now;
TimeSpan duration = DateTime.Now - dt_start2; TimeSpan duration = DateTime.Now - dt_start2;
logger.Information($@"sensor step1 insert sensor_history_hour OK duration={duration.TotalSeconds} ");
arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor hour", "1", "", rowCT.ToString(), conn, cmd); arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor hour", "1", "", rowCT.ToString(), conn, cmd);
// irrDayHour計算 // irrDayHour計算
@ -380,13 +387,17 @@ namespace solarApp.Service
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d')
)b on a.reportdate = b.reportdate"; )b on a.reportdate = b.reportdate";
//conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 }); //conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 });
dt_start2 = DateTime.Now;
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID); cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
cmd.Parameters.AddWithValue("@date1", _date1); cmd.Parameters.AddWithValue("@date1", _date1);
cmd.CommandText = sql; cmd.CommandText = sql;
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); cmd.Parameters.Clear();
duration = DateTime.Now - dt_start2;
arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_day", "insert sensor_day", "1", "", rowCT.ToString(), conn, cmd); arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_day", "insert sensor_day", "1", "", rowCT.ToString(), conn, cmd);
logger.Information($@"sensor step1 insert sensor day OK duration={duration.TotalSeconds} ");
#endregion day #endregion day
#region month 2 #region month 2
@ -440,8 +451,12 @@ namespace solarApp.Service
cmd.Parameters.Clear(); cmd.Parameters.Clear();
arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor_hour", "1", "", rowCT.ToString(), conn, cmd); arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor_hour", "1", "", rowCT.ToString(), conn, cmd);
logger.Information($@"sensor step1 insert sensoravg_history_hour OK duration={duration.TotalSeconds} ");
//日照計 update //日照計 update
sql = string.Empty; sql = string.Empty;
dt_start2 = DateTime.Now;
foreach (var item in irrlst) foreach (var item in irrlst)
{ {
sql += @" update sensoravg_history_hour a join sql += @" update sensoravg_history_hour a join
@ -460,6 +475,9 @@ namespace solarApp.Service
cmd.CommandText = sql; cmd.CommandText = sql;
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); cmd.Parameters.Clear();
duration = DateTime.Now - dt_start2;
logger.Information($@"sensor step1 update sensoravg_history_hour irrAvg OK duration={duration.TotalSeconds} ");
} }
#endregion #endregion
@ -479,11 +497,17 @@ namespace solarApp.Service
where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1 where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1
group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d');"; group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d');";
//conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 }); //conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 });
// dt_start2 = DateTime.Now;
// duration = DateTime.Now - dt_start2;
dt_start2 = DateTime.Now;
cmd.Parameters.AddWithValue("@powerStationID", _powerStationID); cmd.Parameters.AddWithValue("@powerStationID", _powerStationID);
cmd.Parameters.AddWithValue("@date1", _date1); cmd.Parameters.AddWithValue("@date1", _date1);
cmd.CommandText = sql; cmd.CommandText = sql;
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
cmd.Parameters.Clear(); cmd.Parameters.Clear();
duration = DateTime.Now - dt_start2;
logger.Information($@"sensor step1 INSERT sensoravg_history_day OK duration={duration.TotalSeconds} ");
//日照計 update //日照計 update
sql = string.Empty; sql = string.Empty;

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using Serilog;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace solarApp.Service
{
internal class testLog
{
private ILogger logger = Log.ForContext<testLog>();
//private ILogger logger;
//public ILogger MyLoger
//{
// get
// {
// if (logger == null)
// {
// logger = Log.ForContext<testLog>();
// }
// return logger;
// }
//}
public void procOne() {
logger.Information("call procOne OK");
}
}
}

View File

@ -9,6 +9,8 @@ using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using solarApp.Service; using solarApp.Service;
using solarApp.Model; using solarApp.Model;
using Serilog;
using Serilog.Events;
//using System.Text.RegularExpressions; //using System.Text.RegularExpressions;
namespace solarApp namespace solarApp
@ -26,6 +28,9 @@ namespace solarApp
InitializeComponent(); InitializeComponent();
} }
private ILogger logger = Log.ForContext<fmArchive>();
private void bt_inv_proc_Click(object sender, EventArgs e) private void bt_inv_proc_Click(object sender, EventArgs e)
{ {
@ -103,6 +108,7 @@ namespace solarApp
f2.Dock = DockStyle.Fill; f2.Dock = DockStyle.Fill;
f2.Show(); f2.Show();
#endregion #endregion
logger.Information("start winform process @" + System.DateTime.Now.ToString());
} }
private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)