Skip to content

BSL262 — Incorrect use of the method "WriteLogEvent"

Summary

Incorrect use of the method "WriteLogEvent"

Identifiers

Field Value
Rule code BSL262
Compatible alias UsageWriteLogEvent
Severity INFORMATION
Enabled by default Yes
Implemented Yes
Tags standard, badpractice

Behavior

  • The public identifier BSL262 and alias UsageWriteLogEvent are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL262"]
ignore = ["UsageWriteLogEvent"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL262
  • bsl-disable:
Value = "example";  // bsl-disable: BSL262
  • compatible BSLLS form:
Value = "example";  // BSLLS:UsageWriteLogEvent-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL262
// code without this diagnostic
// noqa-enable: BSL262

// bsl-disable: BSL262
// code without this diagnostic
// bsl-enable: BSL262

// BSLLS:UsageWriteLogEvent-off
// code without this diagnostic
// BSLLS:UsageWriteLogEvent-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

It is important to specify the parameters correctly when writing to the Log event.

You can't hide exceptions. При обработке исключений нужно выполнять запись в журнал регистрации с подробным представлением ошибки. To do this, add to the event comment the result DetailErrorDescription(ErrorInfo())

Do not skip the 2nd parameter Log level. If you do not specify it, by default 1C will apply the Information error level, and this record may be lost in the stream of records.

The 5th parameter - a comment to the event of writing to the logging log - must not be omitted either.

Examples

Examples of Invalid Code

    WriteLogEvent("Event");// error
    WriteLogEvent("Event", EventLogLevel.Error);// error
    WriteLogEvent("Event", EventLogLevel.Error, , );// error
    WriteLogEvent("Event", , , , DetailErrorDescription(ErrorInfo()));

    WriteLogEvent("Event", EventLogLevel.Error, , , );// error

    Try
      ServerCode();
    Except
      WriteLogEvent("Event", EventLogLevel.Error, , ,
        ErrorDescription()); // error
      WriteLogEvent("Event", EventLogLevel.Error, , ,
        "Commentary 1"); // error
    EndTry;

Correct code

    Try
      ServerCode();
    Except

      ErrorText = DetailErrorDescription(ErrorInfo());
      WriteLogEvent(NStr("en = 'Performing an operation'"), EventLogLevel.Error, , ,
         ErrorText);
    EndTry;

    Try
      ServerCode();
    Except

      ErrorText = DetailErrorDescription(ErrorInfo());
      WriteLogEvent(NStr("en = 'Performing an operation'"), EventLogLevel.Error, , ,
         ErrorText);

      Raise;
    EndTry;
If an outer attempt makes a log entry, then there is no need to do it again in a nested attempt:
Процедура ЗагрузитьДанные() Экспорт
    Попытка
        ВыполнитьЗаписьДанных();
    Исключение
        ЗаписьЖурналаРегистрации(); // <- исключение подавляется с записью в ЖР
    КонецПопытки;
КонецПроцедуры

Процедура ВыполнитьЗаписьДанных()
    НачатьТранзакцию();
    Попытка
        // ...
        ЗафиксироватьТранзакцию();
    Исключение
        ОтменитьТранзакцию();
        ВызватьИсключение; // <- вложенная попытка, запись в ЖР не требуется
    КонецПопытки;
КонецПроцедуры

Sources