WPF+EntityFrameworkで簡単にログを残すソースの公開 -
WPF+EntityFrameworkで簡単にログを残すソースの公開
How to use to PutActionLog on WPF + EntityFramework
EntityFrameworkなら、ソースからデータベースの作成、テーブルの作成が自動で行えるので、下記のソースさえコピペして貰えれば、データベースが、Postgresだろうが、SqlServerだろうが、Oracleだろうが自動的に全部使えるようになると思います。
DataAccess.PutActionLogにて、IPアドレス、コンピュータ名等モロモロと、引数で渡した情報を+アルファを記録しています。
ActionLog.cs モデル(Model)
context:コンテキスト
DataAccess.cs
Thank you for Reading
How to use to PutActionLog on WPF + EntityFramework
DataAccess.PutActionLogにて、IPアドレス、コンピュータ名等モロモロと、引数で渡した情報を+アルファを記録しています。
ActionLog.cs モデル(Model)
using System;
using System.Collections.Generic;
public partial class ActionLog
{
public int ActionLogID { get; set; }
public string SystemName { get; set; }
public string SystemVersion { get; set; }
public string IpAddress { get; set; }
public string TerminalID { get; set; }
public string UserID { get; set; }
public string FunctionName { get; set; }
public string Condition { get; set; }
public string ErrorText { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public Nullable<System.DateTime> UpdateDate { get; set; }
public string Property1 { get; set; }
}
using System.Collections.Generic;
public partial class ActionLog
{
public int ActionLogID { get; set; }
public string SystemName { get; set; }
public string SystemVersion { get; set; }
public string IpAddress { get; set; }
public string TerminalID { get; set; }
public string UserID { get; set; }
public string FunctionName { get; set; }
public string Condition { get; set; }
public string ErrorText { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public Nullable<System.DateTime> UpdateDate { get; set; }
public string Property1 { get; set; }
}
context:コンテキスト
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MYSystemEntities : DbContext
{
public MYSystemEntities()
: base("name=MYSystemEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<ActionLog> ActionLogs { get; set; }
}
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MYSystemEntities : DbContext
{
public MYSystemEntities()
: base("name=MYSystemEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<ActionLog> ActionLogs { get; set; }
}
DataAccess.cs
public void PutActionLog(String LoginUser, String strFunctionName, String strCondition)
{
using (var context = new MYSystemEntities())
{
System.Net.IPAddress ipaddres = null;
System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ipAddr in ipHostEntry.AddressList)
{
if (ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipaddres = ipAddr;
break;
}
}
context.ActionLogs.Add(new ActionLog
{
SystemName = System.Windows.Application.ResourceAssembly.GetName().Name,
SystemVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
IpAddress = ipaddres.ToString(),
TerminalID = System.Net.Dns.GetHostName(),
FunctionName = strFunctionName,
Condition = strCondition,
UserID = LoginUser,
UpdateDate = DateTime.Now,
CreateDate = DateTime.Now
});
context.SaveChanges();
}
}
{
using (var context = new MYSystemEntities())
{
System.Net.IPAddress ipaddres = null;
System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ipAddr in ipHostEntry.AddressList)
{
if (ipAddr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipaddres = ipAddr;
break;
}
}
context.ActionLogs.Add(new ActionLog
{
SystemName = System.Windows.Application.ResourceAssembly.GetName().Name,
SystemVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),
IpAddress = ipaddres.ToString(),
TerminalID = System.Net.Dns.GetHostName(),
FunctionName = strFunctionName,
Condition = strCondition,
UserID = LoginUser,
UpdateDate = DateTime.Now,
CreateDate = DateTime.Now
});
context.SaveChanges();
}
}
Thank you for Reading