From df2654fb57ea74de0ca15152771d851eaffd2a65 Mon Sep 17 00:00:00 2001 From: jiahao Date: Mon, 30 Oct 2023 08:54:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20log=20=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/StationReportController.cs | 6 +- solarApp/Program.cs | 8 +- solarApp/Service/archiveLowData.cs | 20 +- solarApp/Service/getStationSvc.cs | 12 +- solarApp/Service/procArchiveSensorHourly.cs | 51 +++-- solarApp/Service/procInvSvc.cs | 188 ++++++++---------- solarApp/Service/procSensorSvc.cs | 34 +++- solarApp/Service/testLog.cs | 28 +++ solarApp/fmArchive.cs | 6 + 9 files changed, 202 insertions(+), 151 deletions(-) create mode 100644 solarApp/Service/testLog.cs diff --git a/SolarPower/Controllers/StationReportController.cs b/SolarPower/Controllers/StationReportController.cs index 172f302..f245a64 100644 --- a/SolarPower/Controllers/StationReportController.cs +++ b/SolarPower/Controllers/StationReportController.cs @@ -486,9 +486,9 @@ namespace SolarPower.Controllers vForDataCount++; object vv = ""; - avghour = Convert.ToString( dbody["tothour"]) ?? "NaN"; - avgKWHKWP = Convert.ToString(dbody["KWHKWP"]) ?? "NaN"; - avgdayKWH = Convert.ToString(dbody["dayKWH"]) ?? "NaN"; + avghour = (dbody["tothour"] == null) ? "NaN" : dbody["tothour"].ToString(); + avgKWHKWP = (dbody["KWHKWP"] == null) ? "NaN" : dbody["KWHKWP"].ToString(); + avgdayKWH = (dbody["dayKWH"] == null) ? "NaN" : dbody["dayKWH"].ToString(); //if (dbody.TryGetValue("tothour", out vv)) // avghour = (Convert.ToDouble(dbody["tothour"].ToString()) + Convert.ToDouble(avghour)).ToString(); diff --git a/solarApp/Program.cs b/solarApp/Program.cs index 2373c34..5f23d69 100644 --- a/solarApp/Program.cs +++ b/solarApp/Program.cs @@ -64,14 +64,20 @@ namespace solarApp // Console.WriteLine("Error Occured"); // } // } + // ref: https://www.ruyut.com/2021/10/serilog.html Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() // 設定最低顯示層級 預設: Information - .WriteTo.File("Logs/solarApp/log-.log", + .WriteTo.File("Logs/log-.log", rollingInterval: RollingInterval.Day, // 每天一個檔案 outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u5}] {Message:lj}{NewLine}{Exception}" ) // 輸出到檔案 檔名範例: log-20211005.log .CreateLogger(); + //Log.Information("Init log"); + + // Log.Debug("debug"); + + Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); diff --git a/solarApp/Service/archiveLowData.cs b/solarApp/Service/archiveLowData.cs index 626af20..a8afd6c 100644 --- a/solarApp/Service/archiveLowData.cs +++ b/solarApp/Service/archiveLowData.cs @@ -5,15 +5,16 @@ using MySql.Data.MySqlClient; using solarApp.Model; using System.Configuration; using Dapper; -using Microsoft.Extensions.Logging; +//using Microsoft.Extensions.Logging; using System.Windows.Forms; +using Serilog; namespace solarApp.Service { class archiveLowData { string Connection1 = string.Empty; - + private ILogger logger = Log.ForContext(); public archiveLowData(string Connection_parame = null) { if (!string.IsNullOrEmpty(Connection_parame)) @@ -42,17 +43,16 @@ namespace solarApp.Service // string yyyymm = System.DateTime.Now.AddMonths(-2).ToString("yyyy-MM"); _siteID = siteID; //祉宏隞 - 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.AddMonths(-1).ToString("yyyy-MM")+ "-01"; + move_month = System.DateTime.Now.ToString("yyyy-MM") + "-01"; // + //_date1 = date1; get_siteInfo(); checkTable(); moveData_station(); moveData_inv(); - moveData_sensor(); - - + moveData_sensor(); result = true; return result; @@ -301,7 +301,7 @@ namespace solarApp.Service //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`) 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); // System.Diagnostics.Debug.WriteLine($"ArchiveRowData{_siteDB}.s{_siteID}_station銝鞈撖怠亥逢_siteDB}_history.s{_siteID}_station撖怠交慮dateTime}"); @@ -315,7 +315,9 @@ namespace solarApp.Service //芷文祉鞈 string for_delete_station = $@"DELETE FROM {_siteDB}.s{_siteID01}_station WHERE FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') < '{move_month}'"; - rowCT = conn.Execute(for_delete_station, commandTimeout: 600); + //string for_delete_station = $@"DELETE FROM {_siteDB}.s{_siteID01}_station + // WHERE date(CrdTime) < '{move_month}'"; + 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()} 蝑"); duration = DateTime.Now - dt_start1; diff --git a/solarApp/Service/getStationSvc.cs b/solarApp/Service/getStationSvc.cs index 6622020..62873e5 100644 --- a/solarApp/Service/getStationSvc.cs +++ b/solarApp/Service/getStationSvc.cs @@ -5,6 +5,7 @@ using MySql.Data.MySqlClient; using Dapper; using solarApp.Model; using System.Configuration; +using Serilog; namespace solarApp.Service { @@ -14,6 +15,9 @@ namespace solarApp.Service public class getStationSvc { public string Connection1 { get; set; } + + private ILogger logger = Log.ForContext(); + public getStationSvc(string Connection_parame = null) { if (!string.IsNullOrEmpty(Connection_parame)) @@ -37,7 +41,7 @@ namespace solarApp.Service /// public List get_station_raw(string reportDate, string siteDB, string siteID) { - List ds; + List ds = null; using (MySqlConnection conn = new MySqlConnection(Connection1)) { conn.Open(); @@ -52,7 +56,7 @@ namespace solarApp.Service } catch (Exception ex) { - throw ex; + logger.Error(@$"get_station_raw瑁憭望 {siteDB}.s{siteID} _station 肚reportDate} - [Exception]嚗" + ex.ToString()); } return ds; } @@ -104,7 +108,7 @@ namespace solarApp.Service } catch (Exception ex) { - throw ex; + logger.Error(@$"get_station_rawAvg瑁憭望 {siteDB}.s{siteID} _station 肚date1} - {date2} - [Exception]嚗" + ex.ToString()); } return ds; @@ -209,7 +213,7 @@ namespace solarApp.Service conn.Open(); // 01 批嗅函楊 string sql = @" select id, CompanyId, `code` , SerialNumber, CONCAT(`code` ,'01') SiteID, SiteDB, `name` SiteName from power_station - where deleted = 0 and `status`= 1 and id <> 32 "; + where deleted = 0 and `status`= 1 and id <> 32 "; if (include32) { sql = @" select id, CompanyId, `code` , SerialNumber, CONCAT(`code` ,'01') SiteID, SiteDB, `name` SiteName diff --git a/solarApp/Service/procArchiveSensorHourly.cs b/solarApp/Service/procArchiveSensorHourly.cs index 70756ae..1f45b75 100644 --- a/solarApp/Service/procArchiveSensorHourly.cs +++ b/solarApp/Service/procArchiveSensorHourly.cs @@ -6,7 +6,8 @@ using Dapper; using solarApp.Model; using System.Configuration; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; +//using Microsoft.Extensions.Logging; +using Serilog; namespace solarApp.Service { @@ -14,6 +15,18 @@ namespace solarApp.Service { string Connection1 = string.Empty; ILogger _logger; + public ILogger MyLoger + { + get + { + if (_logger == null) + { + //閬瘛餃雿輻函敶蝐餃蝛粹游蝘,銝霂,銝曄內 + _logger = Log.ForContext(); + } + return _logger; + } + } public string _siteID { get; set; } public string _siteDB { get; set; } public string _siteID01 { get; set; } @@ -44,7 +57,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly憪瑁[{0}]肚1}敺餌蝺刻", _siteID, _date1); + _logger.Information("ProcArchiveSensorHourly憪瑁[{0}]肚1}敺餌蝺刻", _siteID, _date1); } #region 敺 PowerStationID @@ -60,7 +73,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly瑁摰[{0}]肚1}敺餌蝺刻 - {2}", _siteID, _date1, _powerStationID); + _logger.Information("ProcArchiveSensorHourly瑁摰[{0}]肚1}敺餌蝺刻 - {2}", _siteID, _date1, _powerStationID); } #endregion @@ -72,8 +85,8 @@ namespace solarApp.Service { if (_logger != null) { - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}敺餌蝺刻", _siteID, _date1); - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}敺餌蝺刻 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}敺餌蝺刻", _siteID, _date1); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}敺餌蝺刻 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); } throw ex; @@ -95,7 +108,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly憪瑁[{0}]肚1}閮剖撖怠亥銵函鞈", _siteID, _date1); + _logger.Information("ProcArchiveSensorHourly憪瑁[{0}]肚1}閮剖撖怠亥銵函鞈", _siteID, _date1); } string sql = $@" select x.*, z.itemname, z.HourType, z.DayType, z.MonthType,z.SetWhat @@ -161,7 +174,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly瑁摰[{0}]肚1}閮剖撖怠亥銵函鞈", _siteID, _date1); + _logger.Information("ProcArchiveSensorHourly瑁摰[{0}]肚1}閮剖撖怠亥銵函鞈", _siteID, _date1); } conn.Close(); @@ -172,8 +185,8 @@ namespace solarApp.Service { if (_logger != null) { - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖撖怠亥銵函鞈", _siteID, _date1); - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖撖怠亥銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖撖怠亥銵函鞈", _siteID, _date1); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖撖怠亥銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); } throw ex; } @@ -209,8 +222,8 @@ namespace solarApp.Service { if (_logger != null) { - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}亦扯湔啗唾銵函鞈", _siteID, _date1); - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}亦扯湔啗唾銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}亦扯湔啗唾銵函鞈", _siteID, _date1); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}亦扯湔啗唾銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); } throw ex; } @@ -233,7 +246,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly憪瑁[{0}]肚1}蝝舐亦批榆湔啗唾銵函鞈", _siteID, _date1); + _logger.Information("ProcArchiveSensorHourly憪瑁[{0}]肚1}蝝舐亦批榆湔啗唾銵函鞈", _siteID, _date1); } //蝝舐亦 string sql = $@" select IrrDay @@ -281,7 +294,7 @@ namespace solarApp.Service conn.Execute(ss, commandTimeout: 600); if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly瑁摰[{0}]肚1}蝝舐亦批榆湔啗唾銵函鞈", _siteID, _date1); + _logger.Information("ProcArchiveSensorHourly瑁摰[{0}]肚1}蝝舐亦批榆湔啗唾銵函鞈", _siteID, _date1); } conn.Close(); } @@ -290,8 +303,8 @@ namespace solarApp.Service { if (_logger != null) { - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}蝝舐亦批榆湔啗銵函鞈", _siteID, _date1); - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}蝝舐亦批榆湔啗銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}蝝舐亦批榆湔啗銵函鞈", _siteID, _date1); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}蝝舐亦批榆湔啗銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); } throw ex; } @@ -309,7 +322,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly憪瑁[{0}]肚1}閮剖皜方銵函鞈", _siteID, _date1); + _logger.Information("ProcArchiveSensorHourly憪瑁[{0}]肚1}閮剖皜方銵函鞈", _siteID, _date1); } string d1 = _date1 + ":00:00"; string clearTime = Convert.ToDateTime(d1).ToString("yyyy-MM-dd"); @@ -325,7 +338,7 @@ namespace solarApp.Service if (_logger != null) { - _logger.LogInformation("ProcArchiveSensorHourly瑁摰[{0}]肚1}閮剖皜方銵函鞈", _siteID, _date1); + _logger.Information("ProcArchiveSensorHourly瑁摰[{0}]肚1}閮剖皜方銵函鞈", _siteID, _date1); } conn.Close(); @@ -336,8 +349,8 @@ namespace solarApp.Service { if (_logger != null) { - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖皜方銵函鞈", _siteID, _date1); - _logger.LogError("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖皜方銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖皜方銵函鞈", _siteID, _date1); + _logger.Error("ProcArchiveSensorHourly瑁憭望[{0}]肚1}閮剖皜方銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); } throw ex; } diff --git a/solarApp/Service/procInvSvc.cs b/solarApp/Service/procInvSvc.cs index fab16bb..80ef61a 100644 --- a/solarApp/Service/procInvSvc.cs +++ b/solarApp/Service/procInvSvc.cs @@ -6,7 +6,9 @@ using Dapper; using solarApp.Model; using System.Configuration; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; +//using Microsoft.Extensions.Logging; +using Serilog; +using Serilog.Events; //using solarApp.Service.proArchiveLog; namespace solarApp.Service @@ -14,10 +16,10 @@ namespace solarApp.Service public class procInvSvc { string Connection1 = string.Empty; - ILogger _logger; + private ILogger logger = Log.ForContext(); + - - public procInvSvc(string Connection_parame = null, ILogger logger = null) + public procInvSvc(string Connection_parame = null) { if (!string.IsNullOrEmpty(Connection_parame)) { @@ -28,10 +30,10 @@ namespace solarApp.Service Connection1 = ConfigurationManager.ConnectionStrings["mySql"].ConnectionString; } - if (logger != null) - { - _logger = logger; - } + //if (logger != null) + //{ + // _logger = logger; + //} } public string _siteID { get; set; } @@ -52,10 +54,10 @@ namespace solarApp.Service conn.Open(); try { - if (_logger != null) - { - _logger.LogInformation("ProcInvSvc憪瑁[{0}]肚1}霈冽方銵函鞈", _siteID, _date1); - } + //if (_logger != null) + //{ + // logger.Information("ProcInvSvc憪瑁[{0}]肚1}霈冽方銵函鞈", _siteID, _date1); + //} //string sql = @" // delete from inverter_history_15min where powerstationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1; @@ -76,20 +78,15 @@ namespace solarApp.Service cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); - - if (_logger != null) - { - _logger.LogInformation("ProcInvSvc瑁摰[{0}]肚1}霈冽方銵函鞈", _siteID, _date1); - } + + logger.Information(@$"ProcInvSvc瑁摰[{_siteID}] {_date1} 霈冽方銵函鞈"); result = true; } catch (Exception ex) { - if (_logger != null) - { - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈冽方銵函鞈", _siteID, _date1); - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈冽方銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); - } + + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈冽方銵函鞈", _siteID, _date1); + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈冽方銵函鞈 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); //throw ex; arclog.insert_log(_powerStationID, "step0 del inv ", 0, "inv", "del inv error", "0", "", "", conn, cmd); @@ -136,11 +133,9 @@ namespace solarApp.Service { conn.Open(); try - { - if (_logger != null) - { - _logger.LogInformation("ProcInvSvc憪瑁[{0}]肚1}霈典敺餌蝺刻", _siteID, _date1); - } + { + + logger.Information("ProcInvSvc憪瑁[{0}]肚1}霈典敺餌蝺刻", _siteID, _date1); #region 敺 PowerStationID string sql = @" select id , `code` siteID, siteDB, `name` siteName @@ -153,10 +148,8 @@ namespace solarApp.Service _siteID01 = item.SiteID + "01"; } - if (_logger != null) - { - _logger.LogInformation("ProcInvSvc瑁摰[{0}]肚1}霈典敺餌蝺刻 - {2}", _siteID, _date1, _powerStationID); - } + + logger.Information("ProcInvSvc瑁摰[{0}]肚1}霈典敺餌蝺刻 - {2}", _siteID, _date1, _powerStationID); #endregion @@ -164,11 +157,10 @@ namespace solarApp.Service } catch (Exception ex) { - if (_logger != null) - { - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈典敺餌蝺刻", _siteID, _date1); - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈典敺餌蝺刻 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); - } + + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈典敺餌蝺刻", _siteID, _date1); + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈典敺餌蝺刻 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); + arclog.insert_log(_powerStationID, "step0 select stationID ", 0, "stationID", "select stationID", "0", "", "", conn, null); } conn.Close(); @@ -188,29 +180,26 @@ namespace solarApp.Service conn.Execute(ss); string sql = string.Empty; - #region 15 min - try - { - if (_logger != null) - { - _logger.LogInformation("ProcInvSvc憪瑁[{0}]肚1}霈15min鋆", _siteID, _date1); - } + #region 15 min + try + { + logger.Information("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 select * from " + _siteDB + ".s" + _siteID01 + @"_inv a WHERE left(crdTime, 10) = @date1; # 2. add index ALTER TABLE del_Inv_day_" + _powerStationID + @"_s1 ADD INDEX `temp_index` (crdTime, inverterid);"; - //int ct = conn.Execute(sql, new { date1 = _date1 }); - cmd.Connection = conn; - cmd.CommandTimeout = 360; - cmd.Parameters.AddWithValue("@date1", _date1); - cmd.CommandText = sql; - cmd.ExecuteNonQuery(); - cmd.Parameters.Clear(); + //int ct = conn.Execute(sql, new { date1 = _date1 }); + cmd.Connection = conn; + cmd.CommandTimeout = 360; + cmd.Parameters.AddWithValue("@date1", _date1); + cmd.CommandText = sql; + cmd.ExecuteNonQuery(); + cmd.Parameters.Clear(); - sql = @" + sql = @" INSERT INTO solar_master.inverter_history_15min( PowerStationId, TIMESTAMP, INVERTERID, KWH, TODAYKWH, KWHKWP) select PowerStationId, reportdate, a.inverterid, KWH, TODAYKWH, (kwh/(capacity/4)) kwpkwp from ( @@ -238,7 +227,7 @@ namespace solarApp.Service where FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d') = @date1 and right(FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H:%i') , 2) between '45' and '55' group by FROM_UNIXTIME(`TIMESTAMP`/1000,'%Y-%m-%d %H') , inverterid ) a join " + _siteDB + @".inverter b on a.inverterid = b.inverterid"; - + //var ds = conn.Execute(sql, new { date1 = _date1, PowerStationID = _powerStationID }); cmd.Parameters.AddWithValue("@date1", _date1); @@ -248,26 +237,21 @@ namespace solarApp.Service cmd.Parameters.Clear(); cmd.Dispose(); - if (_logger != null) - _logger.LogInformation("ProcInvSvc瑁摰[{0}]肚1}霈15min鋆", _siteID, _date1); - } - catch (Exception ex) - { - if (_logger != null) - { - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈15min鋆", _siteID, _date1); - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈15min鋆 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); - } + logger.Information("ProcInvSvc瑁摰[{0}]肚1}霈15min鋆", _siteID, _date1); + } + catch (Exception ex) + { + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈15min鋆", _siteID, _date1); + logger.Error("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); } #endregion #region hour try - { - if (_logger != null) - _logger.LogInformation("ProcInvSvc憪瑁[{0}]肚1}霈玖our鋆", _siteID, _date1); + { + logger.Information("ProcInvSvc憪瑁[{0}]肚1}霈玖our鋆", _siteID, _date1); sql = @$"DROP TEMPORARY TABLE IF EXISTS Inv_day_" + _powerStationID + @"_s2; CREATE TEMPORARY TABLE Inv_day_" + _powerStationID + @"_s2 @@ -322,9 +306,8 @@ namespace solarApp.Service ON a.reportDate = inv_pr.reportDate AND a.INVERTERID = inv_pr.INVERTERID;"; //ct = conn.Execute(sql, new { date1 = _date1 }); cmd.CommandText = sql; - cmd.ExecuteNonQuery(); - if (_logger != null) - _logger.LogInformation("ProcInvSvc瑁摰[{0}]肚1}霈玖our鋆", _siteID, _date1); + cmd.ExecuteNonQuery(); + logger.Information("ProcInvSvc瑁摰[{0}]肚1}霈玖our鋆", _siteID, _date1); sql = @$"DROP TABLE IF EXISTS del_Inv_day_" + _powerStationID + @"_s1; "; //conn.Execute(ss); @@ -333,11 +316,9 @@ namespace solarApp.Service } catch (Exception ex) { - if (_logger != null) - { - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈玖our鋆", _siteID, _date1); - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈玖our鋆 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); - } + + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈玖our鋆", _siteID, _date1); + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈玖our鋆 - [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); // throw ex; } @@ -347,10 +328,8 @@ namespace solarApp.Service #region day try { - if (_logger != null) - { - _logger.LogInformation("ProcInvSvc憪瑁[{0}]肚1}霈牡ay鋆", _siteID, _date1); - } + + logger.Information("ProcInvSvc憪瑁[{0}]肚1}霈牡ay鋆", _siteID, _date1); sql = @" 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.Dispose(); - if (_logger != null) - _logger.LogInformation("ProcInvSvc瑁摰[{0}]肚1}霈牡ay鋆", _siteID, _date1); + + logger.Information("ProcInvSvc瑁摰[{0}]肚1}霈牡ay鋆", _siteID, _date1); } catch (Exception ex) { - if (_logger != null) - { - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈牡ay鋆", _siteID, _date1); - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈牡ay鋆 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); - } + + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈牡ay鋆", _siteID, _date1); + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈牡ay鋆 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); //throw ex; 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 try { - if (_logger != null) - _logger.LogInformation("ProcInvSvc憪瑁[{0}]肚1}霈皂onth鋆", _siteID, _date1); + + logger.Information("ProcInvSvc憪瑁[{0}]肚1}霈皂onth鋆", _siteID, _date1); sql = @" 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.Parameters.Clear(); - if (_logger != null) - { - _logger.LogInformation("ProcInvSvc瑁摰[{0}]肚1}霈皂onth鋆", _siteID, _date1); - } + + logger.Information("ProcInvSvc瑁摰[{0}]肚1}霈皂onth鋆", _siteID, _date1); } catch (Exception ex) { - if (_logger != null) - { - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈皂onth鋆", _siteID, _date1); - _logger.LogError("ProcInvSvc瑁憭望[{0}]肚1}霈皂onth鋆 - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); - } + + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈皂onth鋆", _siteID, _date1); + logger.Error("ProcInvSvc瑁憭望[{0}]肚1}霈皂onth鋆 - [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); // throw ex; } @@ -513,10 +486,8 @@ namespace solarApp.Service #region get SQL //try //{ - if (_logger != null) - { - _logger.LogInformation("Proc_inv_亙 憪瑁[{0}]肚1}inv_get_SQLStatement", _siteID, _date1); - } + + logger.Information("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); #region 撱箇 temp table 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);"; //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 { @@ -541,7 +512,7 @@ namespace solarApp.Service var rowCT = cmd.ExecuteScalar(); //select LAST_INSERT_ID() 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) @@ -665,9 +636,8 @@ set a.inv_02 = b.inv_02 "); cmd.CommandText = sql; cmd.ExecuteNonQuery(); - - if (_logger != null) - _logger.LogInformation("Proc_inv_亙 table 瑁摰[{0}]肚1} inv_get_SQLStatement", _siteID, _date1); + + logger.Information("Proc_inv_亙 table 瑁摰[{0}]肚1} inv_get_SQLStatement", _siteID, _date1); 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); @@ -677,11 +647,9 @@ set a.inv_02 = b.inv_02 "); { 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); - if (_logger != null) - { - _logger.LogError("Proc_inv_亙 table瑁憭望[{0}]肚1}inv_get_SQLStatement", _siteID, _date1); - _logger.LogError("Proc_inv_亙 table瑁憭望[{0}]肚1}inv_get_SQLStatement - [Exception]嚗{2}", _siteID, _date1, ex.ToString()); - } + + logger.Error("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()); } #endregion diff --git a/solarApp/Service/procSensorSvc.cs b/solarApp/Service/procSensorSvc.cs index 60ec67d..d7e4fdd 100644 --- a/solarApp/Service/procSensorSvc.cs +++ b/solarApp/Service/procSensorSvc.cs @@ -9,6 +9,8 @@ using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; using MySqlX.XDevAPI.Relational; using static solarApp.Model.weather_model; +using Serilog; +using Serilog.Events; namespace solarApp.Service { @@ -16,6 +18,8 @@ namespace solarApp.Service { string Connection1 = string.Empty; + private ILogger logger = Log.ForContext(); + public procSensorSvc(string Connection_parame = null) { if (!string.IsNullOrEmpty(Connection_parame)) @@ -136,12 +140,14 @@ namespace solarApp.Service cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); conn.Close(); + logger.Information("sensor_history clearData OK"); } result = true; } catch (Exception ex) - { + { //throw ex; + logger.Error("sensor_history error: " + ex.Message); } return result; } @@ -294,6 +300,7 @@ namespace solarApp.Service //cmd.Parameters() //cmd.ExecuteNonQuery(); //conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 }); + DateTime dt_start2 = DateTime.Now; cmd.Connection = conn; cmd.CommandTimeout = 720; @@ -302,9 +309,9 @@ namespace solarApp.Service cmd.CommandText = sql; int rowCT = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); - DateTime dt_start2 = DateTime.Now; TimeSpan duration = DateTime.Now - dt_start2; - arclog.insert_log(_powerStationID, "sensor step1", duration.TotalSeconds, "sensor_history_hour", "insert sensor hour", "1", "", rowCT.ToString(), conn, cmd); + 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); // irrDayHour閮蝞 //sql = $@"update sensor_history_hour a join ( @@ -380,13 +387,17 @@ namespace solarApp.Service group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') )b on a.reportdate = b.reportdate"; //conn.Execute(sql, new { powerStationID = _powerStationID, date1 = _date1 }); + dt_start2 = DateTime.Now; + cmd.Parameters.AddWithValue("@powerStationID", _powerStationID); cmd.Parameters.AddWithValue("@date1", _date1); cmd.CommandText = sql; cmd.ExecuteNonQuery(); 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); - + + logger.Information($@"sensor step1 insert sensor day OK duration={duration.TotalSeconds} "); #endregion day #region month 憒頝冽 2蝑鞈 @@ -440,8 +451,12 @@ namespace solarApp.Service cmd.Parameters.Clear(); 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 sql = string.Empty; + dt_start2 = DateTime.Now; + foreach (var item in irrlst) { sql += @" update sensoravg_history_hour a join @@ -460,6 +475,9 @@ namespace solarApp.Service cmd.CommandText = sql; cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); + + duration = DateTime.Now - dt_start2; + logger.Information($@"sensor step1 update sensoravg_history_hour irrAvg OK duration={duration.TotalSeconds} "); } #endregion @@ -479,11 +497,17 @@ namespace solarApp.Service where powerStationID = @powerStationID and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') = @date1 group by DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d');"; //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("@date1", _date1); cmd.CommandText = sql; 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 sql = string.Empty; diff --git a/solarApp/Service/testLog.cs b/solarApp/Service/testLog.cs new file mode 100644 index 0000000..2e1142f --- /dev/null +++ b/solarApp/Service/testLog.cs @@ -0,0 +1,28 @@ +嚜簑sing 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(); + //private ILogger logger; + //public ILogger MyLoger + //{ + // get + // { + // if (logger == null) + // { + // logger = Log.ForContext(); + // } + // return logger; + // } + //} + public void procOne() { + logger.Information("call procOne OK"); + } + } +} diff --git a/solarApp/fmArchive.cs b/solarApp/fmArchive.cs index 82d83d3..9e54489 100644 --- a/solarApp/fmArchive.cs +++ b/solarApp/fmArchive.cs @@ -9,6 +9,8 @@ using System.Text; using System.Windows.Forms; using solarApp.Service; using solarApp.Model; +using Serilog; +using Serilog.Events; //using System.Text.RegularExpressions; namespace solarApp @@ -26,6 +28,9 @@ namespace solarApp InitializeComponent(); } + private ILogger logger = Log.ForContext(); + + private void bt_inv_proc_Click(object sender, EventArgs e) { @@ -103,6 +108,7 @@ namespace solarApp f2.Dock = DockStyle.Fill; f2.Show(); #endregion + logger.Information("start winform process @" + System.DateTime.Now.ToString()); } private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)