Repository: dotnet/runtime
File: src/coreclr/vm/excep.cpp (C++)

Code before:
                                          _Inout_updates_(buf_size) WCHAR *buf,
                                          const int buf_size,
                                          BOOL sendWindowsEventLog)
{
    GCPROTECT_BEGIN(throwable);
    if (throwable != NULL)
    {
        if (FAILED(CCompRC::LoadString(IDS_EE_UNHANDLED_EXCEPTION, buf, buf_size)))
        {
            wcsncpy_s(buf, buf_size, SZ_UNHANDLED_EXCEPTION, SZ_UNHANDLED_EXCEPTION_CHARLEN);
        }

        SString message(buf);
        SString exceptionMessage = GetExceptionMessageWrapper(pThread, throwable);

        message.Append(W(" "));
        if (!exceptionMessage.IsEmpty())
        {
            message.Append(exceptionMessage);
        }

        PrintToStdErrW(message.GetUnicode());
        PrintToStdErrA("\n");

#ifdef HOST_ANDROID
        EEPolicy::CallstackForFatalErrorLogged();
#endif

#if defined(FEATURE_EVENT_TRACE) && !defined(TARGET_UNIX)
        // Send the log to Windows Event Log
        if (sendWindowsEventLog && ShouldLogInEventLog())
        {
            EX_TRY
            {
                EventReporter reporter(EventReporter::ERT_UnhandledException);

                if (IsException(throwable->GetMethodTable()))
                {
                    if (!exceptionMessage.IsEmpty())
                    {
                        reporter.AddDescription(exceptionMessage);
                    }
                    reporter.Report();
                }
                else
                {
                    StackSString s;
                    TypeString::AppendType(s, TypeHandle(throwable->GetMethodTable()), TypeString::FormatNamespace | TypeString::FormatFullInst);
                    reporter.AddDescription(s);
                    LogCallstackForEventReporter(reporter);
                }

Review comment (bug):
The code seems to make a distinction between unhandled exceptions and fatal error. Unhandled exception is not logged by any of the fatal error methods. Is that correct?

Would it make sense for this to be called like `EEPolicy::CallstackForCrashLogged` to make it more obvious that this is not just for fatal errors?

Code after:
                                          _Inout_updates_(buf_size) WCHAR *buf,
                                          const int buf_size,
                                          BOOL sendWindowsEventLog)
{
    GCPROTECT_BEGIN(throwable);
    if (throwable != NULL)
    {
        if (FAILED(CCompRC::LoadString(IDS_EE_UNHANDLED_EXCEPTION, buf, buf_size)))
        {
            wcsncpy_s(buf, buf_size, SZ_UNHANDLED_EXCEPTION, SZ_UNHANDLED_EXCEPTION_CHARLEN);
        }

        SString message(buf);
        SString exceptionMessage = GetExceptionMessageWrapper(pThread, throwable);

        message.Append(W(" "));
        if (!exceptionMessage.IsEmpty())
        {
            message.Append(exceptionMessage);
        }

        PrintToStdErrW(message.GetUnicode());
        PrintToStdErrA("\n");

#if defined(FEATURE_EVENT_TRACE) && !defined(TARGET_UNIX)
        // Send the log to Windows Event Log
        if (sendWindowsEventLog && ShouldLogInEventLog())
        {
            EX_TRY
            {
                EventReporter reporter(EventReporter::ERT_UnhandledException);

                if (IsException(throwable->GetMethodTable()))
                {
                    if (!exceptionMessage.IsEmpty())
                    {
                        reporter.AddDescription(exceptionMessage);
                    }
                    reporter.Report();
                }
                else
                {
                    StackSString s;
                    TypeString::AppendType(s, TypeHandle(throwable->GetMethodTable()), TypeString::FormatNamespace | TypeString::FormatFullInst);
                    reporter.AddDescription(s);
                    LogCallstackForEventReporter(reporter);
                }
            }
            EX_CATCH
            {
            }