User Tools

Site Tools


nebollogger.entityframework

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
nebollogger.entityframework [2021/08/02 16:02] nebolnebollogger.entityframework [2021/08/07 02:10] (current) nebol
Line 4: Line 4:
  
 See the nuget page  at https://nuget.nebol.se for version history etc. See the nuget page  at https://nuget.nebol.se for version history etc.
- 
-Despite the name it only supports (currently as of 1.0.0.12) .NET Standard 2.1 and .NET Core 3.0 and 3.1 
  
 It creates and uses a couple of tables: It creates and uses a couple of tables:
Line 11: Line 9:
   * LogMachine - stores details on the machine that logs the entry (name, IP etc)   * LogMachine - stores details on the machine that logs the entry (name, IP etc)
   * LogApplication - stores details on the application that logs the entry (name etc)   * LogApplication - stores details on the application that logs the entry (name etc)
-  * LogComponent - stores details on the component that logs the entry (source file, line etc)+  * LogComponent - stores details on the component (source file) that logs the entry 
   * LogEntry - the actual log entry   * LogEntry - the actual log entry
  
 +=== Initialization in .NET Standard / .NET Core for MS SQL Server ===
  
-=== Initialization in .NET Standard / .NET Core ===+Create the empty database.  
 + 
 +EF Migrations do not work (not yet in any case) so you need to create the schema contents either by running the script that is added to your project when installing the package (in the Database Scripts folder) or by calling Database.EnsureCreated() in your startup code. 
 + 
 +If there are other tables in your database than the NebolLogger tables, the Database.EnsureCreated() call will fail as it requires an empty table on first call. In that scenario you should use the script. 
 + 
 +<code> 
 +Install-Package Microsoft.EntityFrameworkCore.SqlServer 
 +</code> 
 + 
 +<code> 
 +using Microsoft.EntityFrameworkCore; 
 +using NebolLogger; 
 +using NebolLogger.Destinations.Database; 
 +using NebolLogger.EntityFramework; 
 +using NebolLogger.Filters; 
 +</code>
  
 <code> <code>
Line 21: Line 36:
 { {
  var optionsBuilder = new DbContextOptionsBuilder<NebolLoggerDbContext>();  var optionsBuilder = new DbContextOptionsBuilder<NebolLoggerDbContext>();
- optionsBuilder.UseSqlServer(@"server=mssql.local;user id=sa;password=!Q2w#E4rqq;database=mixer_logg");+ optionsBuilder.UseSqlServer(connection_string);
  
  using (var db = new NebolLoggerDbContext(optionsBuilder.Options))  using (var db = new NebolLoggerDbContext(optionsBuilder.Options))
- db.Database.Migrate(); + db.Database.EnsureCreated(); 
- //db.Database.EnsureCreated();+
  
  Destination_Database db_dest = new Destination_Database(optionsBuilder.Options);  Destination_Database db_dest = new Destination_Database(optionsBuilder.Options);
- db_dest.Filters.Add(new Filter_Level(LogType.information));+ 
 + db_dest.Filters.Add(new Filter_Level(LogType.information)); // skip trace messages
  
  Logger.Instance.Destinations.Add(db_dest);  Logger.Instance.Destinations.Add(db_dest);
 } }
 +</code>
 +
 +=== Initialization in .NET Framework for MS SQL Server ===
 +
 +Create the empty database. 
 +
 +EF Migrations do not work (not yet in any case) so you need to create the schema contents by running the script that is added to your project when installing the package (in the Database Scripts folder)  
 +
 +<code>
 +Install-Package Microsoft.EntityFramework
 +</code>
 +
 +<code>
 +using NebolLogger;
 +using NebolLogger.Destinations.Database;
 +using NebolLogger.EntityFramework;
 +using NebolLogger.Filters;
 +</code>
 +
 +<code>
 +static void SetupLogging()
 +{
 + Destination_Database db_dest = new Destination_Database(connection_string);
 +
 + db_dest.Filters.Add(new Filter_Level(LogType.information)); // skip trace messages
 +
 + Logger.Instance.Destinations.Add(db_dest);
 +}
 +</code>
 +
 +=== Another possible option for DB creation: Using Microsoft.EntityFramework.Tools ===
 +
 +To prepare database, run in Package Manager:
 +
 +<code>
 +update-database -ConfigurationTypeName NebolLogger.EntityFramework.NebolLoggerDbContextConfiguration -Verbose
 +</code>
 +
 +or if you're using MySQL:
 +
 +<code>
 +update-database -ConfigurationTypeName NebolLogger.EntityFramework.NebolLoggerDbContextConfiguration_MySQL -Verbose
 </code> </code>
  
nebollogger.entityframework.1627912931.txt.gz · Last modified: 2021/08/02 16:02 by nebol