메뉴 접속로그 로직 개선 불필요한Task 제거

This commit is contained in:
iyak 2025-04-14 04:24:33 +00:00
parent 3d993bf121
commit e82b3c3ccf
2 changed files with 54 additions and 48 deletions

View File

@ -13,33 +13,46 @@ namespace NP.Base.Auth
{ {
public EMRole[] Roles { get; set; } public EMRole[] Roles { get; set; }
BaseController _base; BaseController _base;
public bool IsFront { get; set; } public bool IsFront { get; set; }
public override void OnActionExecuting(ActionExecutingContext context) public override void OnActionExecuting(ActionExecutingContext context)
{ {
Controller currentControll = context.Controller as Controller; Controller currentControll = context.Controller as Controller;
_base = currentControll as BaseController; _base = currentControll as BaseController;
//if (_base.SUserInfo.UserNo > 0 && Array.Exists(Roles, el => el == GetEMRole(_base.SUserInfo.UserType)))
if (!IsFront) if (!IsFront)
{ {
var cm = _base.GetMENUS.Where(w => w.usertype == _base.SUserInfo.UserType && w.menuurl.Equals(context.HttpContext.Request.Url.AbsolutePath)).OrderByDescending(od => od.menulevel).FirstOrDefault(); var cm = _base.GetMENUS
if (cm != null .Where(w => w.usertype == _base.SUserInfo.UserType && w.menuurl.Equals(context.HttpContext.Request.Url.AbsolutePath))
|| (_base.SUserInfo.UserNo > 0 && (context.HttpContext.Request.Url.AbsolutePath.Equals("/Home/Index/") .OrderByDescending(od => od.menulevel)
|| context.HttpContext.Request.Url.AbsolutePath.Equals("/Home/Index") .FirstOrDefault();
|| context.HttpContext.Request.Url.AbsolutePath.Equals("/Home/")
|| context.HttpContext.Request.Url.AbsolutePath.Equals("/Home") var absolutePath = context.HttpContext.Request.Url.AbsolutePath.ToLower();
|| context.HttpContext.Request.Url.AbsolutePath.Equals("/"))))
if (cm != null || (_base.SUserInfo.UserNo > 0 && (absolutePath == "/" || absolutePath.StartsWith("/home"))))
{ {
_base.ViewBag.Title = (cm ?? new Model.Menu()).menuname ?? "Home"; _base.ViewBag.Title = (cm ?? new Model.Menu()).menuname ?? "Home";
_base.ViewBag.CMenu = cm; _base.ViewBag.CMenu = cm;
_base.ViewBag.CMenuNo = (cm ?? new Model.Menu()).menuno; _base.ViewBag.CMenuNo = (cm ?? new Model.Menu()).menuno;
_base.ViewBag.CHidden = (cm ?? new Model.Menu()).ishid; _base.ViewBag.CHidden = (cm ?? new Model.Menu()).ishid;
var pmenu = (_base.GetMENUS.Where(w => w.menuno == (cm ?? new Model.Menu()).pmenuno).FirstOrDefault() ?? new Model.Menu()); var pmenu = (_base.GetMENUS.Where(w => w.menuno == (cm ?? new Model.Menu()).pmenuno).FirstOrDefault() ?? new Model.Menu());
_base.ViewBag.pmenuno = pmenu.ishid == 1 ? pmenu.pmenuno : pmenu.menuno; _base.ViewBag.pmenuno = pmenu.ishid == 1 ? pmenu.pmenuno : pmenu.menuno;
_base.ViewBag.pmenunos = (cm ?? new Model.Menu()).menurout; _base.ViewBag.pmenunos = (cm ?? new Model.Menu()).menurout;
//접속로그
var rtn = System.Threading.Tasks.Task.Run(async() => Console.WriteLine(await _base.Dao.Log( // 접속로그 (2025-04-14 HS: 불필요한 2중 Task제거 후 await 구문으로 변경)
new NP.Model.PageLog() { uno = _base.SUserInfo.UserNo, uip = _base.GetUserIP(), logsite = 0, menuno = _base.ViewBag.CMenuNo < 1 ? (int?)null : _base.ViewBag.CMenuNo, loginfo = _base.ViewBag.Title }))); var rtn = System.Threading.Tasks.Task.Run(async () => await _base.Dao.Log(
new Model.PageLog()
{
uno = _base.SUserInfo.UserNo,
uip = _base.GetUserIP(),
logsite = 0,
menuno = _base.ViewBag.CMenuNo < 1 ? (int?)null : _base.ViewBag.CMenuNo,
loginfo = _base.ViewBag.Title
})
);
base.OnActionExecuting(context); base.OnActionExecuting(context);
} }
else else
@ -65,13 +78,12 @@ namespace NP.Base.Auth
{ {
switch (userType) switch (userType)
{ {
case 99: case 99: return EMRole.Admin;
return EMRole.Admin; case 1: return EMRole.Student;
case 1:
return EMRole.Student;
} }
return EMRole.Anonymous; return EMRole.Anonymous;
} }
} }
} }

View File

@ -7,6 +7,7 @@ using System.Threading.Tasks;
using NP.Model; using NP.Model;
using System.Collections; using System.Collections;
using NP.Dao.MyBatis; using NP.Dao.MyBatis;
using MySql.Data.MySqlClient;
namespace NP.Dao namespace NP.Dao
{ {
public partial class CommonDao : BaseDao public partial class CommonDao : BaseDao
@ -267,51 +268,44 @@ namespace NP.Dao
} }
return 0; return 0;
} }
/// <summary>
/// 접속로그 등록
/// </summary>
/// <param name="log"></param>
/// <returns></returns>
public async Task<int> Log(PageLog log) public async Task<int> Log(PageLog log)
{ {
int rtn = 0; int rtn = 0;
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(NP.Frame.DaoFactory.Instance.DataSource.ConnectionString))
using (var conn = new MySqlConnection(Frame.DaoFactory.Instance.DataSource.ConnectionString))
{ {
try try
{ {
//int count = 0; await conn.OpenAsync();
MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand("insert into pagelog(logsite,userno,menuno,pno,loginfo,cdt,cno,udt,uno,uip)values(" + log.logsite + "," + log.uno + "," + (log.menuno == null ? "null" : log.menuno.ToString()) + "," + (log.pno == null ? "null" : log.pno.ToString()) + ",'" + (log.loginfo ?? "") + "',now()," + log.uno + ",now()," + log.uno + ",'" + log.uip + "')", conn); using (var command = new MySqlCommand("insert into pagelog(logsite, userno, menuno, pno, loginfo, cdt, cno, udt, uno, uip) values (@logsite, @uno, @menuno, @pno, @loginfo, now(), @uno, now(), @uno, @uip)", conn))
command.CommandType = System.Data.CommandType.Text; {
conn.Open(); command.Parameters.AddWithValue("@logsite", log.logsite);
//conn.OpenAsync(); command.Parameters.AddWithValue("@uno", log.uno);
//command.ExecuteNonQuery(); command.Parameters.AddWithValue("@menuno", log.menuno ?? (object)DBNull.Value);
rtn = (int) await command.ExecuteNonQueryAsync(); command.Parameters.AddWithValue("@pno", log.pno ?? (object)DBNull.Value);
//IAsyncResult result = command.BeginExecuteNonQuery(new AsyncCallback(logcompleted), command); command.Parameters.AddWithValue("@loginfo", log.loginfo ?? "");
//IAsyncResult result = command.BeginExecuteNonQuery(); command.Parameters.AddWithValue("@uip", log.uip);
//while (!result.IsCompleted)
//{ rtn = await command.ExecuteNonQueryAsync();
// Console.WriteLine("Waiting ({0})", count++); }
// // Wait for 1/10 second, so the counter
// // does not consume all available resources
// // on the main thread.
// System.Threading.Thread.Sleep(100);
//}
//Console.WriteLine("Command complete. Affected {0} rows.",
// command.EndExecuteNonQuery(result));
//command.EndExecuteNonQuery(result);
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
//Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message);
}
catch (InvalidOperationException ex)
{
//Console.WriteLine("Error: {0}", ex.Message);
} }
catch (Exception ex) catch (Exception ex)
{ {
// You might want to pass these errors Console.WriteLine(ex.Message);
// back out to the caller.
//Console.WriteLine("Error: {0}", ex.Message);
} }
} }
return rtn; return rtn;
} }
public int SavePay(Pay p, bool inlect, bool inlectsedate = false) public int SavePay(Pay p, bool inlect, bool inlectsedate = false)
{ {
Trans(); Trans();