TBDELogInserterUnit.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. {
  2. Copyright 2005-2006 Log4Delphi Project
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. }
  13. unit TBDELogInserterUnit;
  14. interface
  15. uses
  16. DBTables, TDBLogInserterUnit, TLoggingEventUnit;
  17. type
  18. TBDELogInserter = class (TDBLogInserter)
  19. protected
  20. FDatabase : TDatabase;
  21. FQuery : TQuery;
  22. public
  23. constructor Create(ADatabase: TDatabase; ASQL : String);
  24. destructor Destroy; Override;
  25. procedure Insert(AEvent : TLoggingEvent); Override;
  26. end;
  27. implementation
  28. uses
  29. SysUtils, TLogLogUnit;
  30. constructor TBDELogInserter.Create(ADatabase: TDatabase; ASQL : String);
  31. begin
  32. inherited Create;
  33. FDatabase := ADatabase;
  34. FQuery := TQuery.Create(Nil);
  35. FQuery.DatabaseName := FDatabase.DatabaseName;
  36. FQuery.ParamCheck:=true;
  37. FQuery.SQL.Add(ASQL);
  38. end;
  39. destructor TBDELogInserter.Destroy;
  40. begin
  41. FQuery.Free;
  42. inherited Destroy;
  43. end;
  44. procedure TBDELogInserter.Insert(AEvent : TLoggingEvent);
  45. begin
  46. if not Assigned(FDatabase) then
  47. begin
  48. TLogLog.debug('TBDELogInserter#Insert: Database not Assigned');
  49. exit;
  50. end;
  51. try
  52. FDatabase.Open;
  53. except
  54. on e : Exception do
  55. begin
  56. TLogLog.fatal('TDBXLogInserter#Insert: SqlConnection.Open raised an expeption: '
  57. + e.message);
  58. exit;
  59. end;
  60. end;
  61. if Assigned(FQuery.Params.FindParam('_msg')) then
  62. begin
  63. FQuery.ParamByName('_msg').AsString:=AEvent.getMessage;
  64. end;
  65. if (AEvent.getException<>nil) then
  66. begin
  67. if Assigned(FQuery.Params.FindParam('_exception')) then
  68. FQuery.ParamByName('_exception').AsString:=AEvent.getException.Message;
  69. if Assigned(FQuery.Params.FindParam('_exceptionclass')) then
  70. FQuery.ParamByName('_exceptionclass').AsString :=
  71. AEvent.getException.ClassType.ClassName;
  72. end;
  73. if Assigned(FQuery.Params.FindParam('_startTime')) then
  74. FQuery.ParamByName('_startTime').AsDateTime := AEvent.getStartTime;
  75. if Assigned(FQuery.Params.FindParam('_level')) then
  76. FQuery.ParamByName('_level').AsString:=AEvent.getLevel.toString;
  77. if Assigned(FQuery.Params.FindParam('_levelCode')) then
  78. FQuery.ParamByName('_levelCode').AsInteger:=AEvent.getLevel.intValue;
  79. try
  80. FQuery.ExecSQL();
  81. except
  82. on e : Exception do
  83. begin
  84. TLogLog.fatal('TDbXLogInserter#insert: FQuery.ExecSql raised an expeption: '
  85. + e.message);
  86. end;
  87. end;
  88. FQuery.Close;
  89. end;
  90. end.